Rollup merge of #102889 - petrochenkov:partres, r=cjgillot
rustc_hir: Less error-prone methods for accessing `PartialRes` resolution
This commit is contained in:
commit
cb67283392
@ -205,13 +205,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
let static_def_id = self
|
||||
.resolver
|
||||
.get_partial_res(sym.id)
|
||||
.filter(|res| res.unresolved_segments() == 0)
|
||||
.and_then(|res| {
|
||||
if let Res::Def(DefKind::Static(_), def_id) = res.base_res() {
|
||||
Some(def_id)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
.and_then(|res| res.full_res())
|
||||
.and_then(|res| match res {
|
||||
Res::Def(DefKind::Static(_), def_id) => Some(def_id),
|
||||
_ => None,
|
||||
});
|
||||
|
||||
if let Some(def_id) = static_def_id {
|
||||
|
@ -1044,9 +1044,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
||||
if let ExprKind::Path(qself, path) = &expr.kind {
|
||||
// Does the path resolve to something disallowed in a tuple struct/variant pattern?
|
||||
if let Some(partial_res) = self.resolver.get_partial_res(expr.id) {
|
||||
if partial_res.unresolved_segments() == 0
|
||||
&& !partial_res.base_res().expected_in_tuple_struct_pat()
|
||||
{
|
||||
if let Some(res) = partial_res.full_res() && !res.expected_in_tuple_struct_pat() {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
@ -1066,9 +1064,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
||||
if let ExprKind::Path(qself, path) = &expr.kind {
|
||||
// Does the path resolve to something disallowed in a unit struct/variant pattern?
|
||||
if let Some(partial_res) = self.resolver.get_partial_res(expr.id) {
|
||||
if partial_res.unresolved_segments() == 0
|
||||
&& !partial_res.base_res().expected_in_unit_struct_pat()
|
||||
{
|
||||
if let Some(res) = partial_res.full_res() && !res.expected_in_unit_struct_pat() {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
|
@ -947,7 +947,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
||||
}
|
||||
AssocItemKind::MacCall(..) => unimplemented!(),
|
||||
},
|
||||
trait_item_def_id: self.resolver.get_partial_res(i.id).map(|r| r.base_res().def_id()),
|
||||
trait_item_def_id: self
|
||||
.resolver
|
||||
.get_partial_res(i.id)
|
||||
.map(|r| r.expect_full_res().def_id()),
|
||||
}
|
||||
}
|
||||
|
||||
@ -1349,9 +1352,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
||||
match self
|
||||
.resolver
|
||||
.get_partial_res(bound_pred.bounded_ty.id)
|
||||
.map(|d| (d.base_res(), d.unresolved_segments()))
|
||||
.and_then(|r| r.full_res())
|
||||
{
|
||||
Some((Res::Def(DefKind::TyParam, def_id), 0))
|
||||
Some(Res::Def(DefKind::TyParam, def_id))
|
||||
if bound_pred.bound_generic_params.is_empty() =>
|
||||
{
|
||||
generics
|
||||
|
@ -175,12 +175,7 @@ impl ResolverAstLoweringExt for ResolverAstLowering {
|
||||
return None;
|
||||
}
|
||||
|
||||
let partial_res = self.partial_res_map.get(&expr.id)?;
|
||||
if partial_res.unresolved_segments() != 0 {
|
||||
return None;
|
||||
}
|
||||
|
||||
if let Res::Def(DefKind::Fn, def_id) = partial_res.base_res() {
|
||||
if let Res::Def(DefKind::Fn, def_id) = self.partial_res_map.get(&expr.id)?.full_res()? {
|
||||
// We only support cross-crate argument rewriting. Uses
|
||||
// within the same crate should be updated to use the new
|
||||
// const generics style.
|
||||
@ -753,12 +748,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
}
|
||||
|
||||
fn expect_full_res(&mut self, id: NodeId) -> Res<NodeId> {
|
||||
self.resolver.get_partial_res(id).map_or(Res::Err, |pr| {
|
||||
if pr.unresolved_segments() != 0 {
|
||||
panic!("path not fully resolved: {:?}", pr);
|
||||
}
|
||||
pr.base_res()
|
||||
})
|
||||
self.resolver.get_partial_res(id).map_or(Res::Err, |pr| pr.expect_full_res())
|
||||
}
|
||||
|
||||
fn expect_full_res_from_use(&mut self, id: NodeId) -> impl Iterator<Item = Res<NodeId>> {
|
||||
@ -1138,8 +1128,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
// type and value namespaces. If we resolved the path in the value namespace, we
|
||||
// transform it into a generic const argument.
|
||||
TyKind::Path(ref qself, ref path) => {
|
||||
if let Some(partial_res) = self.resolver.get_partial_res(ty.id) {
|
||||
let res = partial_res.base_res();
|
||||
if let Some(res) = self
|
||||
.resolver
|
||||
.get_partial_res(ty.id)
|
||||
.and_then(|partial_res| partial_res.full_res())
|
||||
{
|
||||
if !res.matches_ns(Namespace::TypeNS) {
|
||||
debug!(
|
||||
"lower_generic_arg: Lowering type argument as const argument: {:?}",
|
||||
@ -1206,8 +1199,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
// by `ty_path`.
|
||||
if qself.is_none()
|
||||
&& let Some(partial_res) = self.resolver.get_partial_res(t.id)
|
||||
&& partial_res.unresolved_segments() == 0
|
||||
&& let Res::Def(DefKind::Trait | DefKind::TraitAlias, _) = partial_res.base_res()
|
||||
&& let Some(Res::Def(DefKind::Trait | DefKind::TraitAlias, _)) = partial_res.full_res()
|
||||
{
|
||||
let (bounds, lifetime_bound) = self.with_dyn_type_scope(true, |this| {
|
||||
let poly_trait_ref = this.ast_arena.ptr.alloc(PolyTraitRef {
|
||||
|
@ -239,7 +239,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
ident: Ident,
|
||||
lower_sub: impl FnOnce(&mut Self) -> Option<&'hir hir::Pat<'hir>>,
|
||||
) -> hir::PatKind<'hir> {
|
||||
match self.resolver.get_partial_res(p.id).map(|d| d.base_res()) {
|
||||
match self.resolver.get_partial_res(p.id).map(|d| d.expect_full_res()) {
|
||||
// `None` can occur in body-less function signatures
|
||||
res @ (None | Some(Res::Local(_))) => {
|
||||
let canonical_id = match res {
|
||||
|
@ -29,11 +29,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
|
||||
let partial_res =
|
||||
self.resolver.get_partial_res(id).unwrap_or_else(|| PartialRes::new(Res::Err));
|
||||
let base_res = partial_res.base_res();
|
||||
let unresolved_segments = partial_res.unresolved_segments();
|
||||
|
||||
let path_span_lo = p.span.shrink_to_lo();
|
||||
let proj_start = p.segments.len() - partial_res.unresolved_segments();
|
||||
let proj_start = p.segments.len() - unresolved_segments;
|
||||
let path = self.arena.alloc(hir::Path {
|
||||
res: self.lower_res(partial_res.base_res()),
|
||||
res: self.lower_res(base_res),
|
||||
segments: self.arena.alloc_from_iter(p.segments[..proj_start].iter().enumerate().map(
|
||||
|(i, segment)| {
|
||||
let param_mode = match (qself_position, param_mode) {
|
||||
@ -46,7 +48,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
_ => param_mode,
|
||||
};
|
||||
|
||||
let parenthesized_generic_args = match partial_res.base_res() {
|
||||
let parenthesized_generic_args = match base_res {
|
||||
// `a::b::Trait(Args)`
|
||||
Res::Def(DefKind::Trait, _) if i + 1 == proj_start => {
|
||||
ParenthesizedGenericArgs::Ok
|
||||
@ -83,7 +85,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
|
||||
// Simple case, either no projections, or only fully-qualified.
|
||||
// E.g., `std::mem::size_of` or `<I as Iterator>::Item`.
|
||||
if partial_res.unresolved_segments() == 0 {
|
||||
if unresolved_segments == 0 {
|
||||
return hir::QPath::Resolved(qself, path);
|
||||
}
|
||||
|
||||
|
@ -464,6 +464,16 @@ impl PartialRes {
|
||||
pub fn unresolved_segments(&self) -> usize {
|
||||
self.unresolved_segments
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn full_res(&self) -> Option<Res<NodeId>> {
|
||||
(self.unresolved_segments == 0).then_some(self.base_res)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn expect_full_res(&self) -> Res<NodeId> {
|
||||
self.full_res().expect("unexpected unresolved segments")
|
||||
}
|
||||
}
|
||||
|
||||
/// Different kinds of symbols can coexist even if they share the same textual name.
|
||||
|
@ -326,7 +326,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
|
||||
}
|
||||
PathResult::Module(..) => Err(VisResolutionError::ModuleOnly(path.span)),
|
||||
PathResult::NonModule(partial_res) => {
|
||||
expected_found_error(partial_res.base_res())
|
||||
expected_found_error(partial_res.expect_full_res())
|
||||
}
|
||||
PathResult::Failed { span, label, suggestion, .. } => {
|
||||
Err(VisResolutionError::FailedToResolve(span, label, suggestion))
|
||||
|
@ -641,8 +641,7 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
|
||||
// Check whether we should interpret this as a bare trait object.
|
||||
if qself.is_none()
|
||||
&& let Some(partial_res) = self.r.partial_res_map.get(&ty.id)
|
||||
&& partial_res.unresolved_segments() == 0
|
||||
&& let Res::Def(DefKind::Trait | DefKind::TraitAlias, _) = partial_res.base_res()
|
||||
&& let Some(Res::Def(DefKind::Trait | DefKind::TraitAlias, _)) = partial_res.full_res()
|
||||
{
|
||||
// This path is actually a bare trait object. In case of a bare `Fn`-trait
|
||||
// object with anonymous lifetimes, we need this rib to correctly place the
|
||||
@ -1929,7 +1928,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
|
||||
match ty.kind {
|
||||
TyKind::ImplicitSelf => true,
|
||||
TyKind::Path(None, _) => {
|
||||
let path_res = self.r.partial_res_map[&ty.id].base_res();
|
||||
let path_res = self.r.partial_res_map[&ty.id].expect_full_res();
|
||||
if let Res::SelfTyParam { .. } | Res::SelfTyAlias { .. } = path_res {
|
||||
return true;
|
||||
}
|
||||
@ -1970,7 +1969,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
|
||||
None
|
||||
}
|
||||
})
|
||||
.map(|res| res.base_res())
|
||||
.map(|res| res.expect_full_res())
|
||||
.filter(|res| {
|
||||
// Permit the types that unambiguously always
|
||||
// result in the same type constructor being used
|
||||
@ -2530,7 +2529,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
|
||||
Finalize::new(trait_ref.ref_id, trait_ref.path.span),
|
||||
);
|
||||
self.diagnostic_metadata.currently_processing_impl_trait = None;
|
||||
if let Some(def_id) = res.base_res().opt_def_id() {
|
||||
if let Some(def_id) = res.expect_full_res().opt_def_id() {
|
||||
new_id = Some(def_id);
|
||||
new_val = Some((self.r.expect_module(def_id), trait_ref.clone()));
|
||||
}
|
||||
@ -2886,7 +2885,10 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
|
||||
}
|
||||
|
||||
fn is_base_res_local(&self, nid: NodeId) -> bool {
|
||||
matches!(self.r.partial_res_map.get(&nid).map(|res| res.base_res()), Some(Res::Local(..)))
|
||||
matches!(
|
||||
self.r.partial_res_map.get(&nid).map(|res| res.expect_full_res()),
|
||||
Some(Res::Local(..))
|
||||
)
|
||||
}
|
||||
|
||||
/// Checks that all of the arms in an or-pattern have exactly the
|
||||
@ -3373,12 +3375,11 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
|
||||
source.defer_to_typeck(),
|
||||
finalize,
|
||||
) {
|
||||
Ok(Some(partial_res)) if partial_res.unresolved_segments() == 0 => {
|
||||
if source.is_expected(partial_res.base_res()) || partial_res.base_res() == Res::Err
|
||||
{
|
||||
Ok(Some(partial_res)) if let Some(res) = partial_res.full_res() => {
|
||||
if source.is_expected(res) || res == Res::Err {
|
||||
partial_res
|
||||
} else {
|
||||
report_errors(self, Some(partial_res.base_res()))
|
||||
report_errors(self, Some(res))
|
||||
}
|
||||
}
|
||||
|
||||
@ -3586,20 +3587,21 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
|
||||
};
|
||||
|
||||
if path.len() > 1
|
||||
&& result.base_res() != Res::Err
|
||||
&& let Some(res) = result.full_res()
|
||||
&& res != Res::Err
|
||||
&& path[0].ident.name != kw::PathRoot
|
||||
&& path[0].ident.name != kw::DollarCrate
|
||||
{
|
||||
let unqualified_result = {
|
||||
match self.resolve_path(&[*path.last().unwrap()], Some(ns), None) {
|
||||
PathResult::NonModule(path_res) => path_res.base_res(),
|
||||
PathResult::NonModule(path_res) => path_res.expect_full_res(),
|
||||
PathResult::Module(ModuleOrUniformRoot::Module(module)) => {
|
||||
module.res().unwrap()
|
||||
}
|
||||
_ => return Ok(Some(result)),
|
||||
}
|
||||
};
|
||||
if result.base_res() == unqualified_result {
|
||||
if res == unqualified_result {
|
||||
let lint = lint::builtin::UNUSED_QUALIFICATIONS;
|
||||
self.r.lint_buffer.buffer_lint(
|
||||
lint,
|
||||
|
@ -968,11 +968,10 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
|
||||
let Some(partial_res) = self.r.partial_res_map.get(&bounded_ty.id) else {
|
||||
return false;
|
||||
};
|
||||
if !(matches!(
|
||||
partial_res.base_res(),
|
||||
hir::def::Res::Def(hir::def::DefKind::AssocTy, _)
|
||||
) && partial_res.unresolved_segments() == 0)
|
||||
{
|
||||
if !matches!(
|
||||
partial_res.full_res(),
|
||||
Some(hir::def::Res::Def(hir::def::DefKind::AssocTy, _))
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
(ty, position, path)
|
||||
@ -986,11 +985,10 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
|
||||
let Some(partial_res) = self.r.partial_res_map.get(&peeled_ty.id) else {
|
||||
return false;
|
||||
};
|
||||
if !(matches!(
|
||||
partial_res.base_res(),
|
||||
hir::def::Res::Def(hir::def::DefKind::TyParam, _)
|
||||
) && partial_res.unresolved_segments() == 0)
|
||||
{
|
||||
if !matches!(
|
||||
partial_res.full_res(),
|
||||
Some(hir::def::Res::Def(hir::def::DefKind::TyParam, _))
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
if let (
|
||||
@ -1518,20 +1516,14 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
|
||||
{
|
||||
// Look for a field with the same name in the current self_type.
|
||||
if let Some(resolution) = self.r.partial_res_map.get(&node_id) {
|
||||
match resolution.base_res() {
|
||||
Res::Def(DefKind::Struct | DefKind::Union, did)
|
||||
if resolution.unresolved_segments() == 0 =>
|
||||
{
|
||||
if let Some(field_names) = self.r.field_names.get(&did) {
|
||||
if field_names
|
||||
.iter()
|
||||
.any(|&field_name| ident.name == field_name.node)
|
||||
{
|
||||
return Some(AssocSuggestion::Field);
|
||||
}
|
||||
if let Some(Res::Def(DefKind::Struct | DefKind::Union, did)) =
|
||||
resolution.full_res()
|
||||
{
|
||||
if let Some(field_names) = self.r.field_names.get(&did) {
|
||||
if field_names.iter().any(|&field_name| ident.name == field_name.node) {
|
||||
return Some(AssocSuggestion::Field);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1883,12 +1883,10 @@ impl<'a> Resolver<'a> {
|
||||
|
||||
match self.maybe_resolve_path(&segments, Some(ns), &parent_scope) {
|
||||
PathResult::Module(ModuleOrUniformRoot::Module(module)) => Some(module.res().unwrap()),
|
||||
PathResult::NonModule(path_res) if path_res.unresolved_segments() == 0 => {
|
||||
Some(path_res.base_res())
|
||||
PathResult::NonModule(path_res) => path_res.full_res(),
|
||||
PathResult::Module(ModuleOrUniformRoot::ExternPrelude) | PathResult::Failed { .. } => {
|
||||
None
|
||||
}
|
||||
PathResult::Module(ModuleOrUniformRoot::ExternPrelude)
|
||||
| PathResult::NonModule(..)
|
||||
| PathResult::Failed { .. } => None,
|
||||
PathResult::Module(..) | PathResult::Indeterminate => unreachable!(),
|
||||
}
|
||||
}
|
||||
@ -1939,12 +1937,8 @@ impl<'a> Resolver<'a> {
|
||||
return None;
|
||||
}
|
||||
|
||||
let partial_res = self.partial_res_map.get(&expr.id)?;
|
||||
if partial_res.unresolved_segments() != 0 {
|
||||
return None;
|
||||
}
|
||||
|
||||
if let Res::Def(def::DefKind::Fn, def_id) = partial_res.base_res() {
|
||||
let res = self.partial_res_map.get(&expr.id)?.full_res()?;
|
||||
if let Res::Def(def::DefKind::Fn, def_id) = res {
|
||||
// We only support cross-crate argument rewriting. Uses
|
||||
// within the same crate should be updated to use the new
|
||||
// const generics style.
|
||||
|
@ -590,9 +590,7 @@ impl<'a> Resolver<'a> {
|
||||
|
||||
let res = if path.len() > 1 {
|
||||
let res = match self.maybe_resolve_path(&path, Some(MacroNS), parent_scope) {
|
||||
PathResult::NonModule(path_res) if path_res.unresolved_segments() == 0 => {
|
||||
Ok(path_res.base_res())
|
||||
}
|
||||
PathResult::NonModule(path_res) if let Some(res) = path_res.full_res() => Ok(res),
|
||||
PathResult::Indeterminate if !force => return Err(Determinacy::Undetermined),
|
||||
PathResult::NonModule(..)
|
||||
| PathResult::Indeterminate
|
||||
@ -692,9 +690,8 @@ impl<'a> Resolver<'a> {
|
||||
Some(Finalize::new(ast::CRATE_NODE_ID, path_span)),
|
||||
None,
|
||||
) {
|
||||
PathResult::NonModule(path_res) if path_res.unresolved_segments() == 0 => {
|
||||
let res = path_res.base_res();
|
||||
check_consistency(self, &path, path_span, kind, initial_res, res);
|
||||
PathResult::NonModule(path_res) if let Some(res) = path_res.full_res() => {
|
||||
check_consistency(self, &path, path_span, kind, initial_res, res)
|
||||
}
|
||||
path_res @ PathResult::NonModule(..) | path_res @ PathResult::Failed { .. } => {
|
||||
let (span, label) = if let PathResult::Failed { span, label, .. } = path_res {
|
||||
|
Loading…
x
Reference in New Issue
Block a user