Skip to content

Commit 54597ba

Browse files
authored
Rollup merge of #95784 - WaffleLapkin:typeof_cool_suggestion, 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
2 parents c0655de + 8412d5d commit 54597ba

File tree

6 files changed

+32
-3
lines changed

6 files changed

+32
-3
lines changed

compiler/rustc_error_messages/locales/en-US/diagnostics.ftl

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ typeck-functional-record-update-on-non-struct =
6262
6363
typeck-typeof-reserved-keyword-used =
6464
`typeof` is a reserved keyword but unimplemented
65+
.suggestion = consider replacing `typeof(...)` with an actual type
6566
.label = reserved keyword
6667
6768
typeck-return-stmt-outside-of-fn-body =

compiler/rustc_typeck/src/astconv/mod.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -2460,8 +2460,16 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
24602460
self.normalize_ty(ast_ty.span, array_ty)
24612461
}
24622462
hir::TyKind::Typeof(ref e) => {
2463-
tcx.sess.emit_err(TypeofReservedKeywordUsed { span: ast_ty.span });
2464-
tcx.type_of(tcx.hir().local_def_id(e.hir_id))
2463+
let ty = tcx.type_of(tcx.hir().local_def_id(e.hir_id));
2464+
let span = ast_ty.span;
2465+
tcx.sess.emit_err(TypeofReservedKeywordUsed {
2466+
span,
2467+
ty,
2468+
opt_sugg: Some((span, Applicability::MachineApplicable))
2469+
.filter(|_| ty.is_suggestable()),
2470+
});
2471+
2472+
ty
24652473
}
24662474
hir::TyKind::Infer => {
24672475
// Infer also appears as the type of arguments or return

compiler/rustc_typeck/src/errors.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Errors emitted by typeck.
2+
use rustc_errors::Applicability;
23
use rustc_macros::SessionDiagnostic;
4+
use rustc_middle::ty::Ty;
35
use rustc_span::{symbol::Ident, Span, Symbol};
46

57
#[derive(SessionDiagnostic)]
@@ -127,10 +129,13 @@ pub struct FunctionalRecordUpdateOnNonStruct {
127129

128130
#[derive(SessionDiagnostic)]
129131
#[error(code = "E0516", slug = "typeck-typeof-reserved-keyword-used")]
130-
pub struct TypeofReservedKeywordUsed {
132+
pub struct TypeofReservedKeywordUsed<'tcx> {
133+
pub ty: Ty<'tcx>,
131134
#[primary_span]
132135
#[label]
133136
pub span: Span,
137+
#[suggestion_verbose(message = "suggestion", code = "{ty}")]
138+
pub opt_sugg: Option<(Span, Applicability)>,
134139
}
135140

136141
#[derive(SessionDiagnostic)]

src/test/ui/error-codes/E0516.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ error[E0516]: `typeof` is a reserved keyword but unimplemented
33
|
44
LL | let x: typeof(92) = 92;
55
| ^^^^^^^^^^ reserved keyword
6+
|
7+
help: consider replacing `typeof(...)` with an actual type
8+
|
9+
LL | let x: i32 = 92;
10+
| ~~~
611

712
error: aborting due to previous error
813

src/test/ui/issues/issue-29184.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ error[E0516]: `typeof` is a reserved keyword but unimplemented
33
|
44
LL | let x: typeof(92) = 92;
55
| ^^^^^^^^^^ reserved keyword
6+
|
7+
help: consider replacing `typeof(...)` with an actual type
8+
|
9+
LL | let x: i32 = 92;
10+
| ~~~
611

712
error: aborting due to previous error
813

src/test/ui/typeof/type_mismatch.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ error[E0516]: `typeof` is a reserved keyword but unimplemented
33
|
44
LL | let b: typeof(a) = 1i8;
55
| ^^^^^^^^^ reserved keyword
6+
|
7+
help: consider replacing `typeof(...)` with an actual type
8+
|
9+
LL | let b: u8 = 1i8;
10+
| ~~
611

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

0 commit comments

Comments
 (0)