mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-16 17:03:35 +00:00
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.
This commit is contained in:
parent
fa72316031
commit
d5440926e2
@ -62,6 +62,7 @@ typeck-functional-record-update-on-non-struct =
|
|||||||
|
|
||||||
typeck-typeof-reserved-keyword-used =
|
typeck-typeof-reserved-keyword-used =
|
||||||
`typeof` is a reserved keyword but unimplemented
|
`typeof` is a reserved keyword but unimplemented
|
||||||
|
.suggestion = consider replacing `typeof(...)` with an actual type
|
||||||
.label = reserved keyword
|
.label = reserved keyword
|
||||||
|
|
||||||
typeck-return-stmt-outside-of-fn-body =
|
typeck-return-stmt-outside-of-fn-body =
|
||||||
|
@ -2462,8 +2462,16 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
|
|||||||
self.normalize_ty(ast_ty.span, array_ty)
|
self.normalize_ty(ast_ty.span, array_ty)
|
||||||
}
|
}
|
||||||
hir::TyKind::Typeof(ref e) => {
|
hir::TyKind::Typeof(ref e) => {
|
||||||
tcx.sess.emit_err(TypeofReservedKeywordUsed { span: ast_ty.span });
|
let ty = tcx.type_of(tcx.hir().local_def_id(e.hir_id));
|
||||||
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 => {
|
hir::TyKind::Infer => {
|
||||||
// Infer also appears as the type of arguments or return
|
// Infer also appears as the type of arguments or return
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
//! Errors emitted by typeck.
|
//! Errors emitted by typeck.
|
||||||
|
use rustc_errors::Applicability;
|
||||||
use rustc_macros::SessionDiagnostic;
|
use rustc_macros::SessionDiagnostic;
|
||||||
|
use rustc_middle::ty::Ty;
|
||||||
use rustc_span::{symbol::Ident, Span, Symbol};
|
use rustc_span::{symbol::Ident, Span, Symbol};
|
||||||
|
|
||||||
#[derive(SessionDiagnostic)]
|
#[derive(SessionDiagnostic)]
|
||||||
@ -127,10 +129,13 @@ pub struct FunctionalRecordUpdateOnNonStruct {
|
|||||||
|
|
||||||
#[derive(SessionDiagnostic)]
|
#[derive(SessionDiagnostic)]
|
||||||
#[error(code = "E0516", slug = "typeck-typeof-reserved-keyword-used")]
|
#[error(code = "E0516", slug = "typeck-typeof-reserved-keyword-used")]
|
||||||
pub struct TypeofReservedKeywordUsed {
|
pub struct TypeofReservedKeywordUsed<'tcx> {
|
||||||
|
pub ty: Ty<'tcx>,
|
||||||
#[primary_span]
|
#[primary_span]
|
||||||
#[label]
|
#[label]
|
||||||
pub span: Span,
|
pub span: Span,
|
||||||
|
#[suggestion_verbose(message = "suggestion", code = "{ty}")]
|
||||||
|
pub opt_sugg: Option<(Span, Applicability)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(SessionDiagnostic)]
|
#[derive(SessionDiagnostic)]
|
||||||
|
Loading…
Reference in New Issue
Block a user