diff options
| author | Maybe Waffle <waffle.lapkin@gmail.com> | 2022-04-07 22:46:53 +0400 |
|---|---|---|
| committer | Maybe Waffle <waffle.lapkin@gmail.com> | 2022-04-07 23:40:32 +0400 |
| commit | d5440926e2832f810cadeccff6e2ca5332e11466 (patch) | |
| tree | 4a3c6a522601fa3a82a38f05382a3d8361661232 | |
| parent | fa723160318fac5f3fa6f4141cfc41c0606a3008 (diff) | |
| download | rust-d5440926e2832f810cadeccff6e2ca5332e11466.tar.gz rust-d5440926e2832f810cadeccff6e2ca5332e11466.zip | |
Suggest replacing typeof(...) with an actual type
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.
| -rw-r--r-- | compiler/rustc_error_messages/locales/en-US/diagnostics.ftl | 1 | ||||
| -rw-r--r-- | compiler/rustc_typeck/src/astconv/mod.rs | 12 | ||||
| -rw-r--r-- | compiler/rustc_typeck/src/errors.rs | 7 |
3 files changed, 17 insertions, 3 deletions
diff --git a/compiler/rustc_error_messages/locales/en-US/diagnostics.ftl b/compiler/rustc_error_messages/locales/en-US/diagnostics.ftl index 336e7a66857..2b1deb34304 100644 --- a/compiler/rustc_error_messages/locales/en-US/diagnostics.ftl +++ b/compiler/rustc_error_messages/locales/en-US/diagnostics.ftl @@ -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 = diff --git a/compiler/rustc_typeck/src/astconv/mod.rs b/compiler/rustc_typeck/src/astconv/mod.rs index b6287031665..42379055fff 100644 --- a/compiler/rustc_typeck/src/astconv/mod.rs +++ b/compiler/rustc_typeck/src/astconv/mod.rs @@ -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)); + 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 diff --git a/compiler/rustc_typeck/src/errors.rs b/compiler/rustc_typeck/src/errors.rs index 0b78aea9f05..1088be5f566 100644 --- a/compiler/rustc_typeck/src/errors.rs +++ b/compiler/rustc_typeck/src/errors.rs @@ -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)] @@ -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)] |
