Skip to content

Commit d684855

Browse files
Helper method for whether ast::Ty contains impl Trait
1 parent 5837518 commit d684855

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/librustc_ast/ast.rs

+21
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;
@@ -1784,6 +1785,26 @@ pub struct Ty {
17841785
pub span: Span,
17851786
}
17861787

1788+
impl Ty {
1789+
/// Returns `true` if the kind of this type or any types contained within are `impl Trait`.
1790+
pub fn contains_impl_trait(&self) -> bool {
1791+
struct ContainsImplTrait(bool);
1792+
1793+
impl<'ast> Visitor<'ast> for ContainsImplTrait {
1794+
fn visit_ty(&mut self, t: &'ast Ty) {
1795+
self.0 |= matches!(t.kind, TyKind::ImplTrait(..));
1796+
if !self.0 {
1797+
walk_ty(self, t);
1798+
}
1799+
}
1800+
}
1801+
1802+
let mut vis = ContainsImplTrait(false);
1803+
vis.visit_ty(self);
1804+
vis.0
1805+
}
1806+
}
1807+
17871808
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
17881809
pub struct BareFnTy {
17891810
pub unsafety: Unsafe,

0 commit comments

Comments
 (0)