From 2ac171f94cca347ff3b1ef4c3e13f3566b087e73 Mon Sep 17 00:00:00 2001 From: Jane Lusby Date: Sat, 26 Sep 2020 16:34:09 -0700 Subject: [PATCH 1/4] try enabling typeof for fun error messages --- compiler/rustc_typeck/src/astconv/mod.rs | 4 ++-- compiler/rustc_typeck/src/collect/type_of.rs | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_typeck/src/astconv/mod.rs b/compiler/rustc_typeck/src/astconv/mod.rs index b6de491911ab7..34e06e2d5a01e 100644 --- a/compiler/rustc_typeck/src/astconv/mod.rs +++ b/compiler/rustc_typeck/src/astconv/mod.rs @@ -2277,9 +2277,9 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { let array_ty = tcx.mk_ty(ty::Array(self.ast_ty_to_ty(&ty), length)); 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 }); - tcx.ty_error() + tcx.type_of(e.hir_id.owner) } hir::TyKind::Infer => { // Infer also appears as the type of arguments or return diff --git a/compiler/rustc_typeck/src/collect/type_of.rs b/compiler/rustc_typeck/src/collect/type_of.rs index d8eea1ad80b0b..74135c49d582a 100644 --- a/compiler/rustc_typeck/src/collect/type_of.rs +++ b/compiler/rustc_typeck/src/collect/type_of.rs @@ -417,12 +417,16 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> { let parent_node = tcx.hir().get(tcx.hir().get_parent_node(hir_id)); match parent_node { Node::Ty(&Ty { kind: TyKind::Array(_, ref constant), .. }) - | Node::Ty(&Ty { kind: TyKind::Typeof(ref constant), .. }) | Node::Expr(&Expr { kind: ExprKind::Repeat(_, ref constant), .. }) if constant.hir_id == hir_id => { tcx.types.usize } + Node::Ty(&Ty { kind: TyKind::Typeof(ref constant), .. }) + if constant.hir_id == hir_id => + { + tcx.typeck(def_id).node_type(constant.hir_id) + } Node::Expr(&Expr { kind: ExprKind::ConstBlock(ref anon_const), .. }) if anon_const.hir_id == hir_id => From a49b746da43625fd789c2cd72d24093525c4ca17 Mon Sep 17 00:00:00 2001 From: Jane Lusby Date: Sat, 26 Sep 2020 17:26:12 -0700 Subject: [PATCH 2/4] cyclesss --- compiler/rustc_typeck/src/astconv/mod.rs | 2 +- compiler/rustc_typeck/src/collect/type_of.rs | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_typeck/src/astconv/mod.rs b/compiler/rustc_typeck/src/astconv/mod.rs index 34e06e2d5a01e..6e7d7f33f7283 100644 --- a/compiler/rustc_typeck/src/astconv/mod.rs +++ b/compiler/rustc_typeck/src/astconv/mod.rs @@ -2279,7 +2279,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { } hir::TyKind::Typeof(ref e) => { tcx.sess.emit_err(TypeofReservedKeywordUsed { span: ast_ty.span }); - tcx.type_of(e.hir_id.owner) + tcx.type_of(tcx.hir().local_def_id(e.hir_id)) } hir::TyKind::Infer => { // Infer also appears as the type of arguments or return diff --git a/compiler/rustc_typeck/src/collect/type_of.rs b/compiler/rustc_typeck/src/collect/type_of.rs index 74135c49d582a..51d5f4ebe2bd2 100644 --- a/compiler/rustc_typeck/src/collect/type_of.rs +++ b/compiler/rustc_typeck/src/collect/type_of.rs @@ -422,10 +422,8 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> { { tcx.types.usize } - Node::Ty(&Ty { kind: TyKind::Typeof(ref constant), .. }) - if constant.hir_id == hir_id => - { - tcx.typeck(def_id).node_type(constant.hir_id) + Node::Ty(&Ty { kind: TyKind::Typeof(ref e), .. }) if e.hir_id == hir_id => { + tcx.typeck(def_id).node_type(e.hir_id) } Node::Expr(&Expr { kind: ExprKind::ConstBlock(ref anon_const), .. }) From 81e4d5f96c3065bab1f1590f6c9eb9045924ec0a Mon Sep 17 00:00:00 2001 From: Jane Lusby Date: Fri, 23 Apr 2021 13:28:17 -0700 Subject: [PATCH 3/4] add special case for typeof fallback --- compiler/rustc_typeck/src/check/mod.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/compiler/rustc_typeck/src/check/mod.rs b/compiler/rustc_typeck/src/check/mod.rs index 80e173de6b6e5..cb4257e05347a 100644 --- a/compiler/rustc_typeck/src/check/mod.rs +++ b/compiler/rustc_typeck/src/check/mod.rs @@ -540,6 +540,12 @@ fn typeck_with_fallback<'tcx>( kind: TypeVariableOriginKind::TypeInference, span, }), + Node::Ty(&hir::Ty { + kind: hir::TyKind::Typeof(ref anon_const), .. + }) if anon_const.hir_id == id => fcx.next_ty_var(TypeVariableOrigin { + kind: TypeVariableOriginKind::TypeInference, + span, + }), Node::Expr(&hir::Expr { kind: hir::ExprKind::InlineAsm(ia), .. }) if ia.operands.iter().any(|(op, _op_sp)| match op { hir::InlineAsmOperand::Const { anon_const } => { From ed903f9b912630f0e5fc92101b3e3bc5c99f558d Mon Sep 17 00:00:00 2001 From: Jane Lusby Date: Mon, 26 Apr 2021 16:36:48 -0700 Subject: [PATCH 4/4] add ui test for new typeof error messages --- src/test/ui/typeof/type_mismatch.rs | 9 +++++++++ src/test/ui/typeof/type_mismatch.stderr | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 src/test/ui/typeof/type_mismatch.rs create mode 100644 src/test/ui/typeof/type_mismatch.stderr diff --git a/src/test/ui/typeof/type_mismatch.rs b/src/test/ui/typeof/type_mismatch.rs new file mode 100644 index 0000000000000..3f8339fa5beaf --- /dev/null +++ b/src/test/ui/typeof/type_mismatch.rs @@ -0,0 +1,9 @@ +// Test that using typeof results in the correct type mismatch errors instead of always assuming +// `usize`, in addition to the pre-existing "typeof is reserved and unimplemented" error +fn main() { + const a: u8 = 1; + let b: typeof(a) = 1i8; + //~^ ERROR `typeof` is a reserved keyword but unimplemented + //~| ERROR mismatched types + //~| expected `u8`, found `i8` +} diff --git a/src/test/ui/typeof/type_mismatch.stderr b/src/test/ui/typeof/type_mismatch.stderr new file mode 100644 index 0000000000000..12fd7c9963cfc --- /dev/null +++ b/src/test/ui/typeof/type_mismatch.stderr @@ -0,0 +1,23 @@ +error[E0516]: `typeof` is a reserved keyword but unimplemented + --> $DIR/type_mismatch.rs:5:12 + | +LL | let b: typeof(a) = 1i8; + | ^^^^^^^^^ reserved keyword + +error[E0308]: mismatched types + --> $DIR/type_mismatch.rs:5:24 + | +LL | let b: typeof(a) = 1i8; + | --------- ^^^ expected `u8`, found `i8` + | | + | expected due to this + | +help: change the type of the numeric literal from `i8` to `u8` + | +LL | let b: typeof(a) = 1u8; + | ^^^ + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0308, E0516. +For more information about an error, try `rustc --explain E0308`.