Auto merge of #77246 - yaahc:typeof-errors, r=oli-obk

try enabling typeof for fun error messages
This commit is contained in:
bors 2021-04-27 04:46:45 +00:00
commit 22b686ad99
5 changed files with 43 additions and 3 deletions

View File

@ -2279,9 +2279,9 @@ fn ast_ty_to_ty_inner(&self, ast_ty: &hir::Ty<'_>, borrowed: bool) -> Ty<'tcx> {
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

View File

@ -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 } => {

View File

@ -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 =>

View File

@ -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`
}

View File

@ -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`.