Skip to content

Commit 5962dac

Browse files
Helper method for whether ast::Ty contains impl Trait
1 parent f9934fd commit 5962dac

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/librustc_ast/ast.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub use UnsafeSource::*;
2525
use crate::ptr::P;
2626
use crate::token::{self, DelimToken};
2727
use crate::tokenstream::{DelimSpan, TokenStream, TokenTree};
28+
use crate::visit::{walk_ty, Visitor};
2829

2930
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
3031
use rustc_data_structures::sync::Lrc;
@@ -1795,6 +1796,26 @@ pub struct Ty {
17951796
pub span: Span,
17961797
}
17971798

1799+
impl Ty {
1800+
/// Returns `true` if the kind of this type or any types contained within are `impl Trait`.
1801+
pub fn contains_impl_trait(&self) -> bool {
1802+
struct ContainsImplTrait(bool);
1803+
1804+
impl<'ast> Visitor<'ast> for ContainsImplTrait {
1805+
fn visit_ty(&mut self, t: &'ast Ty) {
1806+
self.0 |= matches!(t.kind, TyKind::ImplTrait(..));
1807+
if !self.0 {
1808+
walk_ty(self, t);
1809+
}
1810+
}
1811+
}
1812+
1813+
let mut vis = ContainsImplTrait(false);
1814+
vis.visit_ty(self);
1815+
vis.0
1816+
}
1817+
}
1818+
17981819
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
17991820
pub struct BareFnTy {
18001821
pub unsafety: Unsafe,

0 commit comments

Comments
 (0)