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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ typeck-functional-record-update-on-non-struct =

typeck-typeof-reserved-keyword-used =
`typeof` is a reserved keyword but unimplemented
.suggestion = consider replacing `typeof(...)` with an actual type
.label = reserved keyword

typeck-return-stmt-outside-of-fn-body =
Expand Down
12 changes: 10 additions & 2 deletions compiler/rustc_typeck/src/astconv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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

let span = ast_ty.span;
tcx.sess.emit_err(TypeofReservedKeywordUsed {
span,
ty,
opt_sugg: Some((span, Applicability::MachineApplicable))
.filter(|_| ty.is_suggestable()),
});

ty
}
hir::TyKind::Infer => {
// Infer also appears as the type of arguments or return
Expand Down
7 changes: 6 additions & 1 deletion compiler/rustc_typeck/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Errors emitted by typeck.
use rustc_errors::Applicability;
use rustc_macros::SessionDiagnostic;
use rustc_middle::ty::Ty;
use rustc_span::{symbol::Ident, Span, Symbol};

#[derive(SessionDiagnostic)]
Expand Down Expand Up @@ -127,10 +129,13 @@ pub struct FunctionalRecordUpdateOnNonStruct {

#[derive(SessionDiagnostic)]
#[error(code = "E0516", slug = "typeck-typeof-reserved-keyword-used")]
pub struct TypeofReservedKeywordUsed {
pub struct TypeofReservedKeywordUsed<'tcx> {
pub ty: Ty<'tcx>,
#[primary_span]
#[label]
pub span: Span,
#[suggestion_verbose(message = "suggestion", code = "{ty}")]
pub opt_sugg: Option<(Span, Applicability)>,
}

#[derive(SessionDiagnostic)]
Expand Down
5 changes: 5 additions & 0 deletions src/test/ui/error-codes/E0516.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ error[E0516]: `typeof` is a reserved keyword but unimplemented
|
LL | let x: typeof(92) = 92;
| ^^^^^^^^^^ reserved keyword
|
help: consider replacing `typeof(...)` with an actual type
|
LL | let x: i32 = 92;
| ~~~

error: aborting due to previous error

Expand Down
5 changes: 5 additions & 0 deletions src/test/ui/issues/issue-29184.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ error[E0516]: `typeof` is a reserved keyword but unimplemented
|
LL | let x: typeof(92) = 92;
| ^^^^^^^^^^ reserved keyword
|
help: consider replacing `typeof(...)` with an actual type
|
LL | let x: i32 = 92;
| ~~~

error: aborting due to previous error

Expand Down
5 changes: 5 additions & 0 deletions src/test/ui/typeof/type_mismatch.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ error[E0516]: `typeof` is a reserved keyword but unimplemented
|
LL | let b: typeof(a) = 1i8;
| ^^^^^^^^^ reserved keyword
|
help: consider replacing `typeof(...)` with an actual type
|
LL | let b: u8 = 1i8;
| ~~

error[E0308]: mismatched types
--> $DIR/type_mismatch.rs:5:24
Expand Down