Skip to content

Commit 228def7

Browse files
committed
Extract AssocItem handling.
1 parent cdcc53b commit 228def7

File tree

1 file changed

+131
-166
lines changed

1 file changed

+131
-166
lines changed

compiler/rustc_resolve/src/late.rs

Lines changed: 131 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -1732,72 +1732,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
17321732
|this| {
17331733
this.visit_generics(generics);
17341734
walk_list!(this, visit_param_bound, bounds, BoundKind::SuperTraits);
1735-
1736-
let walk_assoc_item =
1737-
|this: &mut Self,
1738-
generics: &Generics,
1739-
kind,
1740-
item: &'ast AssocItem| {
1741-
this.with_generic_param_rib(
1742-
&generics.params,
1743-
AssocItemRibKind,
1744-
LifetimeRibKind::Generics {
1745-
binder: item.id,
1746-
span: generics.span,
1747-
kind,
1748-
},
1749-
|this| {
1750-
visit::walk_assoc_item(this, item, AssocCtxt::Trait)
1751-
},
1752-
);
1753-
};
1754-
1755-
this.with_trait_items(items, |this| {
1756-
for item in items {
1757-
match &item.kind {
1758-
AssocItemKind::Const(_, ty, default) => {
1759-
this.visit_ty(ty);
1760-
// Only impose the restrictions of `ConstRibKind` for an
1761-
// actual constant expression in a provided default.
1762-
if let Some(expr) = default {
1763-
// We allow arbitrary const expressions inside of associated consts,
1764-
// even if they are potentially not const evaluatable.
1765-
//
1766-
// Type parameters can already be used and as associated consts are
1767-
// not used as part of the type system, this is far less surprising.
1768-
this.with_constant_rib(
1769-
IsRepeatExpr::No,
1770-
HasGenericParams::Yes,
1771-
None,
1772-
|this| this.visit_expr(expr),
1773-
);
1774-
}
1775-
}
1776-
AssocItemKind::Fn(box Fn { generics, .. }) => {
1777-
walk_assoc_item(
1778-
this,
1779-
generics,
1780-
LifetimeBinderKind::Function,
1781-
item,
1782-
);
1783-
}
1784-
AssocItemKind::TyAlias(box TyAlias {
1785-
generics,
1786-
..
1787-
}) => {
1788-
walk_assoc_item(
1789-
this,
1790-
generics,
1791-
LifetimeBinderKind::Item,
1792-
item,
1793-
);
1794-
}
1795-
AssocItemKind::MacCall(_) => {
1796-
panic!("unexpanded macro in resolve!")
1797-
}
1798-
};
1799-
}
1800-
});
1735+
this.resolve_trait_items(items);
18011736
},
18021737
);
18031738
},
@@ -2073,16 +2008,53 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
20732008
}
20742009

20752010
/// When evaluating a `trait` use its associated types' idents for suggestions in E0412.
2076-
fn with_trait_items<T>(
2077-
&mut self,
2078-
trait_items: &'ast [P<AssocItem>],
2079-
f: impl FnOnce(&mut Self) -> T,
2080-
) -> T {
2011+
fn resolve_trait_items(&mut self, trait_items: &'ast [P<AssocItem>]) {
20812012
let trait_assoc_items =
20822013
replace(&mut self.diagnostic_metadata.current_trait_assoc_items, Some(&trait_items));
2083-
let result = f(self);
2014+
2015+
let walk_assoc_item =
2016+
|this: &mut Self, generics: &Generics, kind, item: &'ast AssocItem| {
2017+
this.with_generic_param_rib(
2018+
&generics.params,
2019+
AssocItemRibKind,
2020+
LifetimeRibKind::Generics { binder: item.id, span: generics.span, kind },
2021+
|this| visit::walk_assoc_item(this, item, AssocCtxt::Trait),
2022+
);
2023+
};
2024+
2025+
for item in trait_items {
2026+
match &item.kind {
2027+
AssocItemKind::Const(_, ty, default) => {
2028+
self.visit_ty(ty);
2029+
// Only impose the restrictions of `ConstRibKind` for an
2030+
// actual constant expression in a provided default.
2031+
if let Some(expr) = default {
2032+
// We allow arbitrary const expressions inside of associated consts,
2033+
// even if they are potentially not const evaluatable.
2034+
//
2035+
// Type parameters can already be used and as associated consts are
2036+
// not used as part of the type system, this is far less surprising.
2037+
self.with_constant_rib(
2038+
IsRepeatExpr::No,
2039+
HasGenericParams::Yes,
2040+
None,
2041+
|this| this.visit_expr(expr),
2042+
);
2043+
}
2044+
}
2045+
AssocItemKind::Fn(box Fn { generics, .. }) => {
2046+
walk_assoc_item(self, generics, LifetimeBinderKind::Function, item);
2047+
}
2048+
AssocItemKind::TyAlias(box TyAlias { generics, .. }) => {
2049+
walk_assoc_item(self, generics, LifetimeBinderKind::Item, item);
2050+
}
2051+
AssocItemKind::MacCall(_) => {
2052+
panic!("unexpanded macro in resolve!")
2053+
}
2054+
};
2055+
}
2056+
20842057
self.diagnostic_metadata.current_trait_assoc_items = trait_assoc_items;
2085-
result
20862058
}
20872059

20882060
/// This is called to resolve a trait reference from an `impl` (i.e., `impl Trait for Foo`).
@@ -2173,99 +2145,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
21732145
this.with_self_rib_ns(ValueNS, Res::SelfCtor(item_def_id), |this| {
21742146
debug!("resolve_implementation with_self_rib_ns(ValueNS, ...)");
21752147
for item in impl_items {
2176-
use crate::ResolutionError::*;
2177-
match &item.kind {
2178-
AssocItemKind::Const(_default, _ty, _expr) => {
2179-
debug!("resolve_implementation AssocItemKind::Const");
2180-
// If this is a trait impl, ensure the const
2181-
// exists in trait
2182-
this.check_trait_item(
2183-
item.id,
2184-
item.ident,
2185-
&item.kind,
2186-
ValueNS,
2187-
item.span,
2188-
|i, s, c| ConstNotMemberOfTrait(i, s, c),
2189-
);
2190-
2191-
// We allow arbitrary const expressions inside of associated consts,
2192-
// even if they are potentially not const evaluatable.
2193-
//
2194-
// Type parameters can already be used and as associated consts are
2195-
// not used as part of the type system, this is far less surprising.
2196-
this.with_constant_rib(
2197-
IsRepeatExpr::No,
2198-
HasGenericParams::Yes,
2199-
None,
2200-
|this| {
2201-
visit::walk_assoc_item(
2202-
this,
2203-
item,
2204-
AssocCtxt::Impl,
2205-
)
2206-
},
2207-
);
2208-
}
2209-
AssocItemKind::Fn(box Fn { generics, .. }) => {
2210-
debug!("resolve_implementation AssocItemKind::Fn");
2211-
// We also need a new scope for the impl item type parameters.
2212-
this.with_generic_param_rib(
2213-
&generics.params,
2214-
AssocItemRibKind,
2215-
LifetimeRibKind::Generics { binder: item.id, span: generics.span, kind: LifetimeBinderKind::Function },
2216-
|this| {
2217-
// If this is a trait impl, ensure the method
2218-
// exists in trait
2219-
this.check_trait_item(
2220-
item.id,
2221-
item.ident,
2222-
&item.kind,
2223-
ValueNS,
2224-
item.span,
2225-
|i, s, c| MethodNotMemberOfTrait(i, s, c),
2226-
);
2227-
2228-
visit::walk_assoc_item(
2229-
this,
2230-
item,
2231-
AssocCtxt::Impl,
2232-
)
2233-
},
2234-
);
2235-
}
2236-
AssocItemKind::TyAlias(box TyAlias {
2237-
generics, ..
2238-
}) => {
2239-
debug!("resolve_implementation AssocItemKind::TyAlias");
2240-
// We also need a new scope for the impl item type parameters.
2241-
this.with_generic_param_rib(
2242-
&generics.params,
2243-
AssocItemRibKind,
2244-
LifetimeRibKind::Generics { binder: item.id, span: generics.span, kind: LifetimeBinderKind::Item },
2245-
|this| {
2246-
// If this is a trait impl, ensure the type
2247-
// exists in trait
2248-
this.check_trait_item(
2249-
item.id,
2250-
item.ident,
2251-
&item.kind,
2252-
TypeNS,
2253-
item.span,
2254-
|i, s, c| TypeNotMemberOfTrait(i, s, c),
2255-
);
2256-
2257-
visit::walk_assoc_item(
2258-
this,
2259-
item,
2260-
AssocCtxt::Impl,
2261-
)
2262-
},
2263-
);
2264-
}
2265-
AssocItemKind::MacCall(_) => {
2266-
panic!("unexpanded macro in resolve!")
2267-
}
2268-
}
2148+
this.resolve_impl_item(&**item);
22692149
}
22702150
});
22712151
});
@@ -2278,6 +2158,91 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
22782158
});
22792159
}
22802160

2161+
fn resolve_impl_item(&mut self, item: &'ast AssocItem) {
2162+
use crate::ResolutionError::*;
2163+
match &item.kind {
2164+
AssocItemKind::Const(_default, _ty, _expr) => {
2165+
debug!("resolve_implementation AssocItemKind::Const");
2166+
// If this is a trait impl, ensure the const
2167+
// exists in trait
2168+
self.check_trait_item(
2169+
item.id,
2170+
item.ident,
2171+
&item.kind,
2172+
ValueNS,
2173+
item.span,
2174+
|i, s, c| ConstNotMemberOfTrait(i, s, c),
2175+
);
2176+
2177+
// We allow arbitrary const expressions inside of associated consts,
2178+
// even if they are potentially not const evaluatable.
2179+
//
2180+
// Type parameters can already be used and as associated consts are
2181+
// not used as part of the type system, this is far less surprising.
2182+
self.with_constant_rib(IsRepeatExpr::No, HasGenericParams::Yes, None, |this| {
2183+
visit::walk_assoc_item(this, item, AssocCtxt::Impl)
2184+
});
2185+
}
2186+
AssocItemKind::Fn(box Fn { generics, .. }) => {
2187+
debug!("resolve_implementation AssocItemKind::Fn");
2188+
// We also need a new scope for the impl item type parameters.
2189+
self.with_generic_param_rib(
2190+
&generics.params,
2191+
AssocItemRibKind,
2192+
LifetimeRibKind::Generics {
2193+
binder: item.id,
2194+
span: generics.span,
2195+
kind: LifetimeBinderKind::Function,
2196+
},
2197+
|this| {
2198+
// If this is a trait impl, ensure the method
2199+
// exists in trait
2200+
this.check_trait_item(
2201+
item.id,
2202+
item.ident,
2203+
&item.kind,
2204+
ValueNS,
2205+
item.span,
2206+
|i, s, c| MethodNotMemberOfTrait(i, s, c),
2207+
);
2208+
2209+
visit::walk_assoc_item(this, item, AssocCtxt::Impl)
2210+
},
2211+
);
2212+
}
2213+
AssocItemKind::TyAlias(box TyAlias { generics, .. }) => {
2214+
debug!("resolve_implementation AssocItemKind::TyAlias");
2215+
// We also need a new scope for the impl item type parameters.
2216+
self.with_generic_param_rib(
2217+
&generics.params,
2218+
AssocItemRibKind,
2219+
LifetimeRibKind::Generics {
2220+
binder: item.id,
2221+
span: generics.span,
2222+
kind: LifetimeBinderKind::Item,
2223+
},
2224+
|this| {
2225+
// If this is a trait impl, ensure the type
2226+
// exists in trait
2227+
this.check_trait_item(
2228+
item.id,
2229+
item.ident,
2230+
&item.kind,
2231+
TypeNS,
2232+
item.span,
2233+
|i, s, c| TypeNotMemberOfTrait(i, s, c),
2234+
);
2235+
2236+
visit::walk_assoc_item(this, item, AssocCtxt::Impl)
2237+
},
2238+
);
2239+
}
2240+
AssocItemKind::MacCall(_) => {
2241+
panic!("unexpanded macro in resolve!")
2242+
}
2243+
}
2244+
}
2245+
22812246
fn check_trait_item<F>(
22822247
&mut self,
22832248
id: NodeId,

0 commit comments

Comments
 (0)