From fb98fbb759cf7b2e3cb262bf3621407cae9e3c6d Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Tue, 16 Jul 2024 11:31:04 +0000 Subject: [PATCH 1/2] Make `ErrorGuaranteed` discoverable outside types, consts, and lifetimes --- .../rustc_middle/src/ty/structural_impls.rs | 19 ++++++++-- compiler/rustc_type_ir/src/visit.rs | 36 +++++++------------ 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/compiler/rustc_middle/src/ty/structural_impls.rs b/compiler/rustc_middle/src/ty/structural_impls.rs index a9dca47ab43..33c04b41117 100644 --- a/compiler/rustc_middle/src/ty/structural_impls.rs +++ b/compiler/rustc_middle/src/ty/structural_impls.rs @@ -257,7 +257,6 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { crate::ty::adjustment::PointerCoercion, ::rustc_span::Span, ::rustc_span::symbol::Ident, - ::rustc_errors::ErrorGuaranteed, ty::BoundVar, ty::ValTree<'tcx>, } @@ -443,13 +442,14 @@ fn super_visit_with>>(&self, visitor: &mut V) -> V:: pat.visit_with(visitor) } + ty::Error(guar) => guar.visit_with(visitor), + ty::Bool | ty::Char | ty::Str | ty::Int(_) | ty::Uint(_) | ty::Float(_) - | ty::Error(_) | ty::Infer(_) | ty::Bound(..) | ty::Placeholder(..) @@ -602,6 +602,21 @@ fn super_visit_with>>(&self, visitor: &mut V) -> V:: } } +impl<'tcx> TypeVisitable> for rustc_span::ErrorGuaranteed { + fn visit_with>>(&self, visitor: &mut V) -> V::Result { + visitor.visit_error(*self) + } +} + +impl<'tcx> TypeFoldable> for rustc_span::ErrorGuaranteed { + fn try_fold_with>>( + self, + _folder: &mut F, + ) -> Result { + Ok(self) + } +} + impl<'tcx> TypeFoldable> for InferConst { fn try_fold_with>>( self, diff --git a/compiler/rustc_type_ir/src/visit.rs b/compiler/rustc_type_ir/src/visit.rs index 25eb56fe3fb..d604199b8e0 100644 --- a/compiler/rustc_type_ir/src/visit.rs +++ b/compiler/rustc_type_ir/src/visit.rs @@ -101,8 +101,12 @@ fn visit_ty(&mut self, t: I::Ty) -> Self::Result { // The default region visitor is a no-op because `Region` is non-recursive // and has no `super_visit_with` method to call. - fn visit_region(&mut self, _r: I::Region) -> Self::Result { - Self::Result::output() + fn visit_region(&mut self, r: I::Region) -> Self::Result { + if let ty::ReError(guar) = r.kind() { + self.visit_error(guar) + } else { + Self::Result::output() + } } fn visit_const(&mut self, c: I::Const) -> Self::Result { @@ -116,6 +120,10 @@ fn visit_predicate(&mut self, p: I::Predicate) -> Self::Result { fn visit_clauses(&mut self, p: I::Clauses) -> Self::Result { p.super_visit_with(self) } + + fn visit_error(&mut self, _guar: I::ErrorGuaranteed) -> Self::Result { + Self::Result::output() + } } /////////////////////////////////////////////////////////////////////////// @@ -547,27 +555,7 @@ fn visit_clauses(&mut self, clauses: I::Clauses) -> Self::Result { impl TypeVisitor for HasErrorVisitor { type Result = ControlFlow; - fn visit_ty(&mut self, t: ::Ty) -> Self::Result { - if let ty::Error(guar) = t.kind() { - ControlFlow::Break(guar) - } else { - t.super_visit_with(self) - } - } - - fn visit_const(&mut self, c: ::Const) -> Self::Result { - if let ty::ConstKind::Error(guar) = c.kind() { - ControlFlow::Break(guar) - } else { - c.super_visit_with(self) - } - } - - fn visit_region(&mut self, r: ::Region) -> Self::Result { - if let ty::ReError(guar) = r.kind() { - ControlFlow::Break(guar) - } else { - ControlFlow::Continue(()) - } + fn visit_error(&mut self, guar: ::ErrorGuaranteed) -> Self::Result { + ControlFlow::Break(guar) } } From 53f7f8ce5cb01a4bdef70b0bdb4aa812fe548ce7 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Tue, 16 Jul 2024 11:31:18 +0000 Subject: [PATCH 2/2] Remove an unnecessary impl --- compiler/rustc_middle/src/ty/structural_impls.rs | 6 ------ compiler/rustc_type_ir/src/visit.rs | 9 +++++++++ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_middle/src/ty/structural_impls.rs b/compiler/rustc_middle/src/ty/structural_impls.rs index 33c04b41117..00ea87690c1 100644 --- a/compiler/rustc_middle/src/ty/structural_impls.rs +++ b/compiler/rustc_middle/src/ty/structural_impls.rs @@ -632,12 +632,6 @@ fn visit_with>>(&self, _visitor: &mut V) -> V::Resul } } -impl<'tcx> TypeSuperVisitable> for ty::UnevaluatedConst<'tcx> { - fn super_visit_with>>(&self, visitor: &mut V) -> V::Result { - self.args.visit_with(visitor) - } -} - impl<'tcx> TypeVisitable> for TyAndLayout<'tcx, Ty<'tcx>> { fn visit_with>>(&self, visitor: &mut V) -> V::Result { visitor.visit_ty(self.ty) diff --git a/compiler/rustc_type_ir/src/visit.rs b/compiler/rustc_type_ir/src/visit.rs index d604199b8e0..6ec38b78fc2 100644 --- a/compiler/rustc_type_ir/src/visit.rs +++ b/compiler/rustc_type_ir/src/visit.rs @@ -447,6 +447,15 @@ fn visit_clauses(&mut self, clauses: I::Clauses) -> Self::Result { ControlFlow::Continue(()) } } + + #[inline] + fn visit_error(&mut self, _guar: ::ErrorGuaranteed) -> Self::Result { + if self.flags.intersects(TypeFlags::HAS_ERROR) { + ControlFlow::Break(FoundFlags) + } else { + ControlFlow::Continue(()) + } + } } #[derive(Debug, PartialEq, Eq, Copy, Clone)]