Skip to content

Commit b81adab

Browse files
committed
Also recover type for errors in static items in type_of
The const interner uses the body to intern static allocations. This means that an allocation will be mutable if the resulting type contains interior mutability. Const eval contains an assertion that types that are `Freeze` do not have mutable allocations, which was failing for cases where the type was a type error. An alternative fix for this would be to avoid the assertion in const eval for type errors, allowing those to both be mutable and immutable. This is implemented in a follow-up commit as well.
1 parent 14562dd commit b81adab

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

compiler/rustc_hir/src/hir.rs

+18
Original file line numberDiff line numberDiff line change
@@ -2509,6 +2509,24 @@ impl<'hir> Ty<'hir> {
25092509
_ => false,
25102510
}
25112511
}
2512+
2513+
pub fn references_error(&self) -> Option<rustc_span::ErrorGuaranteed> {
2514+
use crate::intravisit::Visitor;
2515+
struct ErrVisitor(Option<rustc_span::ErrorGuaranteed>);
2516+
impl<'v> Visitor<'v> for ErrVisitor {
2517+
fn visit_ty(&mut self, t: &'v Ty<'v>) {
2518+
if let TyKind::Err(guar) = t.kind {
2519+
self.0 = Some(guar);
2520+
return;
2521+
}
2522+
crate::intravisit::walk_ty(self, t);
2523+
}
2524+
}
2525+
2526+
let mut err_visitor = ErrVisitor(None);
2527+
err_visitor.visit_ty(self);
2528+
err_visitor.0
2529+
}
25122530
}
25132531

25142532
/// Not represented directly in the AST; referred to by name through a `ty_path`.

compiler/rustc_hir_analysis/src/collect/type_of.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<Ty
403403

404404
Node::Item(item) => match item.kind {
405405
ItemKind::Static(ty, .., body_id) => {
406-
if ty.is_suggestable_infer_ty() {
406+
if ty.is_suggestable_infer_ty() || ty.references_error().is_some() {
407407
infer_placeholder_type(
408408
tcx,
409409
def_id,

0 commit comments

Comments
 (0)