Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggest replacing typeof(...) with an actual type #95784

Merged
merged 2 commits into from
Apr 10, 2022

Conversation

WaffleLapkin
Copy link
Member

This PR adds suggestion to replace typeof(...) with an actual type of ..., for example in case of typeof(1) we suggest replacing it with i32.

If the expression

  1. Is not const ({ let a = 1; let _: typeof(a); })
  2. Can't be found (let _: typeof(this_variable_does_not_exist))
  3. Or has non-suggestable type (closure, generator, error, etc)
    we don't suggest anything.

The 1 one is sad, but it's not clear how to support non-consts expressions for typeof.

This PR is inspired by this tweet.

This commit suggests replacing typeof(...) with an actual type of "...",
for example in case of `typeof(1)` we suggest replacing it with `i32`.

If the expression
- Is not const (`{ let a = 1; let _: typeof(a); }`)
- Can't be found (`let _: typeof(this_variable_does_not_exist)`)
- Or has non-suggestable type (closure, generator, error, etc)
we don't suggest anything.
@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Apr 7, 2022
@rust-highfive
Copy link
Collaborator

r? @fee1-dead

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 7, 2022
@@ -2462,8 +2462,16 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
self.normalize_ty(ast_ty.span, array_ty)
}
hir::TyKind::Typeof(ref e) => {
tcx.sess.emit_err(TypeofReservedKeywordUsed { span: ast_ty.span });
tcx.type_of(tcx.hir().local_def_id(e.hir_id))
let ty = tcx.type_of(tcx.hir().local_def_id(e.hir_id));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this report a cycle error if we do something like:

fn foo() -> typeof(foo) {}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it does

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hrm.. wonder if that's ez to fix... probably not. should be ok, since this is illegal syntax anyways.

Copy link
Member

@fee1-dead fee1-dead Apr 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this ICE? If then it should be fixed. Not necessarily in this PR though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't ICE, it just shows the usual cycle error like "cycle detected when computing function signature of foo"
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=c657d61f3db8f63bce43cffa68c1f85b

@rust-log-analyzer

This comment has been minimized.

@compiler-errors
Copy link
Member

r? @compiler-errors

this suggestion is relatively harmless, and we already have the infrastructure to suggest it (since we parse the inside of typeof as an AnonConst)...
r=me after blessing tests

@bors delegate+

@bors
Copy link
Contributor

bors commented Apr 7, 2022

✌️ @WaffleLapkin can now approve this pull request

@compiler-errors
Copy link
Member

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Apr 10, 2022

📌 Commit 8412d5d has been approved by compiler-errors

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 10, 2022
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this pull request Apr 10, 2022
…, r=compiler-errors

Suggest replacing `typeof(...)` with an actual type

This PR adds suggestion to replace `typeof(...)` with an actual type of `...`, for example in case of `typeof(1)` we suggest replacing it with `i32`.

If the expression
1. Is not const (`{ let a = 1; let _: typeof(a); }`)
2. Can't be found (`let _: typeof(this_variable_does_not_exist)`)
3. Or has non-suggestable type (closure, generator, error, etc)
we don't suggest anything.

The 1 one is sad, but it's not clear how to support non-consts expressions for `typeof`.

_This PR is inspired by [this tweet]._

[this tweet]: https://twitter.com/compiler_errors/status/1511945354752638976
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 10, 2022
Rollup of 7 pull requests

Successful merges:

 - rust-lang#95566 (Avoid duplication of doc comments in `std::char` constants and functions)
 - rust-lang#95784 (Suggest replacing `typeof(...)` with an actual type)
 - rust-lang#95807 (Suggest adding a local for vector to fix borrowck errors)
 - rust-lang#95849 (Check for git submodules in non-git source tree.)
 - rust-lang#95852 (Fix missing space in lossy provenance cast lint)
 - rust-lang#95857 (Allow multiple derefs to be splitted in deref_separator)
 - rust-lang#95868 (rustdoc: Reduce allocations in a `html::markdown` function)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 54597ba into rust-lang:master Apr 10, 2022
@rustbot rustbot added this to the 1.62.0 milestone Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants