Skip to content

Commit fb5d364

Browse files
committed
feat(rustc_middle): Ty::contains_error method
Add a new method on `rustc_middle::Ty` to detect if a type contains any `TyKind::Error`. This is more specific than `TypeVisitableExt::references_error`, which is true in many other situations.
1 parent 42ff2ee commit fb5d364

File tree

1 file changed

+23
-0
lines changed
  • compiler/rustc_middle/src/ty

1 file changed

+23
-0
lines changed

compiler/rustc_middle/src/ty/sty.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,6 +1315,29 @@ impl<'tcx> Ty<'tcx> {
13151315
cf.is_break()
13161316
}
13171317

1318+
/// Checks whether a type recursively contains some `TyKind::Error(_)`.
1319+
///
1320+
/// Example: The (erroneous) type `Vec<{type error}, {type error}>` returns
1321+
/// true.
1322+
pub fn contains_error(self) -> bool {
1323+
struct ContainsErrorVisitor;
1324+
1325+
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ContainsErrorVisitor {
1326+
type Result = ControlFlow<()>;
1327+
1328+
fn visit_ty(&mut self, t: Ty<'tcx>) -> Self::Result {
1329+
if let ty::Error(..) = t.kind() {
1330+
ControlFlow::Break(())
1331+
} else {
1332+
t.super_visit_with(self)
1333+
}
1334+
}
1335+
}
1336+
1337+
let cf = self.visit_with(&mut ContainsErrorVisitor);
1338+
cf.is_break()
1339+
}
1340+
13181341
/// Returns the type and mutability of `*ty`.
13191342
///
13201343
/// The parameter `explicit` indicates if this is an *explicit* dereference.

0 commit comments

Comments
 (0)