Skip to content

Commit 8a5847f

Browse files
committed
Already poison the type_of result of the anon const used in the typeof expression
1 parent 870a01a commit 8a5847f

File tree

4 files changed

+22
-24
lines changed

4 files changed

+22
-24
lines changed

compiler/rustc_hir_analysis/src/astconv/mod.rs

+4-18
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::astconv::errors::prohibit_assoc_ty_binding;
1212
use crate::astconv::generics::{check_generic_arg_count, create_args_for_parent_generic_args};
1313
use crate::bounds::Bounds;
1414
use crate::collect::HirPlaceholderCollector;
15-
use crate::errors::{AmbiguousLifetimeBound, TypeofReservedKeywordUsed};
15+
use crate::errors::AmbiguousLifetimeBound;
1616
use crate::middle::resolve_bound_vars as rbv;
1717
use crate::require_c_abi_if_c_variadic;
1818
use rustc_ast::TraitObjectSyntax;
@@ -30,8 +30,8 @@ use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
3030
use rustc_infer::traits::ObligationCause;
3131
use rustc_middle::middle::stability::AllowUnstable;
3232
use rustc_middle::ty::{
33-
self, Const, GenericArgKind, GenericArgsRef, GenericParamDefKind, IsSuggestable, ParamEnv, Ty,
34-
TyCtxt, TypeVisitableExt,
33+
self, Const, GenericArgKind, GenericArgsRef, GenericParamDefKind, ParamEnv, Ty, TyCtxt,
34+
TypeVisitableExt,
3535
};
3636
use rustc_session::lint::builtin::AMBIGUOUS_ASSOCIATED_ITEMS;
3737
use rustc_span::edit_distance::find_best_match_for_name;
@@ -2539,21 +2539,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
25392539

25402540
Ty::new_array_with_const_len(tcx, self.ast_ty_to_ty(ty), length)
25412541
}
2542-
hir::TyKind::Typeof(e) => {
2543-
let ty_erased = tcx.type_of(e.def_id).instantiate_identity();
2544-
let ty = tcx.fold_regions(ty_erased, |r, _| {
2545-
if r.is_erased() { tcx.lifetimes.re_static } else { r }
2546-
});
2547-
let span = ast_ty.span;
2548-
let (ty, opt_sugg) = if let Some(ty) = ty.make_suggestable(tcx, false) {
2549-
(ty, Some((span, Applicability::MachineApplicable)))
2550-
} else {
2551-
(ty, None)
2552-
};
2553-
tcx.dcx().emit_err(TypeofReservedKeywordUsed { span, ty, opt_sugg });
2554-
2555-
ty
2556-
}
2542+
hir::TyKind::Typeof(e) => tcx.type_of(e.def_id).instantiate_identity(),
25572543
hir::TyKind::Infer => {
25582544
// Infer also appears as the type of arguments or return
25592545
// values in an ExprKind::Closure, or as

compiler/rustc_hir_analysis/src/collect/type_of.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use rustc_middle::ty::{self, ImplTraitInTraitData, IsSuggestable, Ty, TyCtxt, Ty
99
use rustc_span::symbol::Ident;
1010
use rustc_span::{Span, DUMMY_SP};
1111

12+
use crate::errors::TypeofReservedKeywordUsed;
13+
1214
use super::bad_placeholder;
1315
use super::ItemCtxt;
1416
pub use opaque::test_opaque_hidden_types;
@@ -39,8 +41,18 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> {
3941
{
4042
return tcx.types.usize;
4143
}
42-
Node::Ty(&hir::Ty { kind: TyKind::Typeof(ref e), .. }) if e.hir_id == hir_id => {
43-
return tcx.typeck(def_id).node_type(e.hir_id);
44+
Node::Ty(&hir::Ty { kind: TyKind::Typeof(ref e), span, .. }) if e.hir_id == hir_id => {
45+
let ty = tcx.typeck(def_id).node_type(e.hir_id);
46+
let ty = tcx.fold_regions(ty, |r, _| {
47+
if r.is_erased() { ty::Region::new_error_misc(tcx) } else { r }
48+
});
49+
let (ty, opt_sugg) = if let Some(ty) = ty.make_suggestable(tcx, false) {
50+
(ty, Some((span, Applicability::MachineApplicable)))
51+
} else {
52+
(ty, None)
53+
};
54+
tcx.dcx().emit_err(TypeofReservedKeywordUsed { span, ty, opt_sugg });
55+
return ty;
4456
}
4557
Node::Expr(&Expr { kind: ExprKind::InlineAsm(asm), .. })
4658
| Node::Item(&Item { kind: ItemKind::GlobalAsm(asm), .. })

tests/rustdoc-ui/issues/issue-102986.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ LL | y: (typeof("hey"),),
66
|
77
help: consider replacing `typeof(...)` with an actual type
88
|
9-
LL | y: (&'static str,),
10-
| ~~~~~~~~~~~~
9+
LL | y: (&str,),
10+
| ~~~~
1111

1212
error: aborting due to 1 previous error
1313

tests/ui/typeof/issue-100183.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ LL | y: (typeof("hey"),),
66
|
77
help: consider replacing `typeof(...)` with an actual type
88
|
9-
LL | y: (&'static str,),
10-
| ~~~~~~~~~~~~
9+
LL | y: (&str,),
10+
| ~~~~
1111

1212
error: aborting due to 1 previous error
1313

0 commit comments

Comments
 (0)