Skip to content

Commit 22b686a

Browse files
committed
Auto merge of rust-lang#77246 - yaahc:typeof-errors, r=oli-obk
try enabling typeof for fun error messages
2 parents 6eb956f + ed903f9 commit 22b686a

File tree

5 files changed

+43
-3
lines changed

5 files changed

+43
-3
lines changed

compiler/rustc_typeck/src/astconv/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2279,9 +2279,9 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
22792279
let array_ty = tcx.mk_ty(ty::Array(self.ast_ty_to_ty(&ty), length));
22802280
self.normalize_ty(ast_ty.span, array_ty)
22812281
}
2282-
hir::TyKind::Typeof(ref _e) => {
2282+
hir::TyKind::Typeof(ref e) => {
22832283
tcx.sess.emit_err(TypeofReservedKeywordUsed { span: ast_ty.span });
2284-
tcx.ty_error()
2284+
tcx.type_of(tcx.hir().local_def_id(e.hir_id))
22852285
}
22862286
hir::TyKind::Infer => {
22872287
// Infer also appears as the type of arguments or return

compiler/rustc_typeck/src/check/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,12 @@ fn typeck_with_fallback<'tcx>(
540540
kind: TypeVariableOriginKind::TypeInference,
541541
span,
542542
}),
543+
Node::Ty(&hir::Ty {
544+
kind: hir::TyKind::Typeof(ref anon_const), ..
545+
}) if anon_const.hir_id == id => fcx.next_ty_var(TypeVariableOrigin {
546+
kind: TypeVariableOriginKind::TypeInference,
547+
span,
548+
}),
543549
Node::Expr(&hir::Expr { kind: hir::ExprKind::InlineAsm(ia), .. })
544550
if ia.operands.iter().any(|(op, _op_sp)| match op {
545551
hir::InlineAsmOperand::Const { anon_const } => {

compiler/rustc_typeck/src/collect/type_of.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -417,12 +417,14 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
417417
let parent_node = tcx.hir().get(tcx.hir().get_parent_node(hir_id));
418418
match parent_node {
419419
Node::Ty(&Ty { kind: TyKind::Array(_, ref constant), .. })
420-
| Node::Ty(&Ty { kind: TyKind::Typeof(ref constant), .. })
421420
| Node::Expr(&Expr { kind: ExprKind::Repeat(_, ref constant), .. })
422421
if constant.hir_id == hir_id =>
423422
{
424423
tcx.types.usize
425424
}
425+
Node::Ty(&Ty { kind: TyKind::Typeof(ref e), .. }) if e.hir_id == hir_id => {
426+
tcx.typeck(def_id).node_type(e.hir_id)
427+
}
426428

427429
Node::Expr(&Expr { kind: ExprKind::ConstBlock(ref anon_const), .. })
428430
if anon_const.hir_id == hir_id =>

src/test/ui/typeof/type_mismatch.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Test that using typeof results in the correct type mismatch errors instead of always assuming
2+
// `usize`, in addition to the pre-existing "typeof is reserved and unimplemented" error
3+
fn main() {
4+
const a: u8 = 1;
5+
let b: typeof(a) = 1i8;
6+
//~^ ERROR `typeof` is a reserved keyword but unimplemented
7+
//~| ERROR mismatched types
8+
//~| expected `u8`, found `i8`
9+
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error[E0516]: `typeof` is a reserved keyword but unimplemented
2+
--> $DIR/type_mismatch.rs:5:12
3+
|
4+
LL | let b: typeof(a) = 1i8;
5+
| ^^^^^^^^^ reserved keyword
6+
7+
error[E0308]: mismatched types
8+
--> $DIR/type_mismatch.rs:5:24
9+
|
10+
LL | let b: typeof(a) = 1i8;
11+
| --------- ^^^ expected `u8`, found `i8`
12+
| |
13+
| expected due to this
14+
|
15+
help: change the type of the numeric literal from `i8` to `u8`
16+
|
17+
LL | let b: typeof(a) = 1u8;
18+
| ^^^
19+
20+
error: aborting due to 2 previous errors
21+
22+
Some errors have detailed explanations: E0308, E0516.
23+
For more information about an error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)