Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

try enabling typeof for fun error messages #77246

Merged
merged 4 commits into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions compiler/rustc_typeck/src/astconv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(tcx.hir().local_def_id(e.hir_id))
}
hir::TyKind::Infer => {
// Infer also appears as the type of arguments or return
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_typeck/src/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 } => {
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_typeck/src/collect/type_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,12 +417,14 @@ 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 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), .. })
if anon_const.hir_id == hir_id =>
Expand Down
9 changes: 9 additions & 0 deletions src/test/ui/typeof/type_mismatch.rs
Original file line number Diff line number Diff line change
@@ -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`
}
23 changes: 23 additions & 0 deletions src/test/ui/typeof/type_mismatch.stderr
Original file line number Diff line number Diff line change
@@ -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`.