mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-02 14:51:48 +00:00
Disallow nested impl traits
This commit is contained in:
parent
cae997e338
commit
4bb623decb
4 changed files with 112 additions and 1 deletions
|
@ -245,7 +245,13 @@ impl TypeRef {
|
|||
// for types are close enough for our purposes to the inner type for now...
|
||||
ast::Type::ForType(inner) => TypeRef::from_ast_opt(ctx, inner.ty()),
|
||||
ast::Type::ImplTraitType(inner) => {
|
||||
TypeRef::ImplTrait(type_bounds_from_ast(ctx, inner.type_bound_list()))
|
||||
if ctx.outer_impl_trait() {
|
||||
// Disallow nested impl traits
|
||||
TypeRef::Error
|
||||
} else {
|
||||
let _guard = ctx.outer_impl_trait_scope(true);
|
||||
TypeRef::ImplTrait(type_bounds_from_ast(ctx, inner.type_bound_list()))
|
||||
}
|
||||
}
|
||||
ast::Type::DynTraitType(inner) => {
|
||||
TypeRef::DynTrait(type_bounds_from_ast(ctx, inner.type_bound_list()))
|
||||
|
|
|
@ -18,6 +18,26 @@ pub struct LowerCtx<'a> {
|
|||
span_map: OnceCell<SpanMap>,
|
||||
ast_id_map: OnceCell<Arc<AstIdMap>>,
|
||||
impl_trait_bounds: RefCell<Vec<Vec<Interned<TypeBound>>>>,
|
||||
// Prevent nested impl traits like `impl Foo<impl Bar>`.
|
||||
outer_impl_trait: RefCell<bool>,
|
||||
}
|
||||
|
||||
pub(crate) struct OuterImplTraitGuard<'a> {
|
||||
ctx: &'a LowerCtx<'a>,
|
||||
old: bool,
|
||||
}
|
||||
|
||||
impl<'a> OuterImplTraitGuard<'a> {
|
||||
fn new(ctx: &'a LowerCtx<'a>, impl_trait: bool) -> Self {
|
||||
let old = ctx.outer_impl_trait.replace(impl_trait);
|
||||
Self { ctx, old }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Drop for OuterImplTraitGuard<'a> {
|
||||
fn drop(&mut self) {
|
||||
self.ctx.outer_impl_trait.replace(self.old);
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> LowerCtx<'a> {
|
||||
|
@ -28,6 +48,7 @@ impl<'a> LowerCtx<'a> {
|
|||
span_map: OnceCell::new(),
|
||||
ast_id_map: OnceCell::new(),
|
||||
impl_trait_bounds: RefCell::new(Vec::new()),
|
||||
outer_impl_trait: RefCell::default(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -42,6 +63,7 @@ impl<'a> LowerCtx<'a> {
|
|||
span_map,
|
||||
ast_id_map: OnceCell::new(),
|
||||
impl_trait_bounds: RefCell::new(Vec::new()),
|
||||
outer_impl_trait: RefCell::default(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -67,4 +89,12 @@ impl<'a> LowerCtx<'a> {
|
|||
pub fn take_impl_traits_bounds(&self) -> Vec<Vec<Interned<TypeBound>>> {
|
||||
self.impl_trait_bounds.take()
|
||||
}
|
||||
|
||||
pub(crate) fn outer_impl_trait(&self) -> bool {
|
||||
*self.outer_impl_trait.borrow()
|
||||
}
|
||||
|
||||
pub(crate) fn outer_impl_trait_scope(&'a self, impl_trait: bool) -> OuterImplTraitGuard<'a> {
|
||||
OuterImplTraitGuard::new(self, impl_trait)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -202,6 +202,8 @@ pub(super) fn lower_generic_args(
|
|||
continue;
|
||||
}
|
||||
if let Some(name_ref) = assoc_type_arg.name_ref() {
|
||||
// Nested impl traits like `impl Foo<Assoc = impl Bar>` are allowed
|
||||
let _guard = lower_ctx.outer_impl_trait_scope(false);
|
||||
let name = name_ref.as_name();
|
||||
let args = assoc_type_arg
|
||||
.generic_arg_list()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue