Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions compiler/rustc_ast_lowering/src/delegation/resolution.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::ops::ControlFlow;

use ast::visit::Visitor;
use hir::def::DefKind;
use hir::def::{DefKind, Res};
use rustc_ast::{self as ast, AssocItemKind, Delegation, DelegationSource, Item, ItemKind, NodeId};
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
use rustc_data_structures::steal::Steal;
Expand All @@ -20,7 +20,8 @@ use crate::delegation::resolution::resolver::DelegationResolver;
use crate::diagnostics::{
AmbiguousDelegationToInherentImpl, CycleInDelegationSignatureResolution,
DelegationAttemptedBlockWithDefsDeletion, DelegationAttemptedBlockWithDefsRelowering,
DelegationBlockSpecifiedWhenNoParams, UnresolvedDelegationCallee,
DelegationBlockSpecifiedWhenNoParams, FailedToResolveDelegationToInherentImpl,
UnresolvedDelegationCallee,
};

/// Simple (hack or heuristic) resolution of some delegations to inherent impls
Expand Down Expand Up @@ -66,8 +67,16 @@ pub(crate) fn resolve_type_relative_delegations(
_ => unreachable!("we are processing only delegations"),
};

let res = r.partial_res_map.get(&delegation.id);
let res = res.and_then(|res| res.base_res().opt_def_id());
let delegation_base_res =
r.partial_res_map.get(&delegation.id).map(|res| res.base_res());
let is_inherent_self = matches!(
delegation_base_res,
Some(Res::SelfTyAlias { is_trait_impl: false, .. })
);
let res = delegation_base_res.and_then(|res| match res {
Res::SelfTyAlias { alias_to, is_trait_impl: false, .. } => Some(alias_to),
other => other.opt_def_id(),
});
let ident = delegation.path.segments.last().map(|s| s.ident);

let span = delegation.last_segment_span();
Expand Down Expand Up @@ -100,6 +109,9 @@ pub(crate) fn resolve_type_relative_delegations(
TypeRelativeDelegationRes::Ok(res.to_def_id())
}
},
_ if is_inherent_self => {
TypeRelativeDelegationRes::InherentFnNotFound
}
_ => default_error_res(),
}
}
Expand Down Expand Up @@ -344,6 +356,9 @@ impl<'tcx> DelegationResolver<'_, 'tcx> {
Some(res) => match *res {
TypeRelativeDelegationRes::Ok(sig_id) => Ok(sig_id),
TypeRelativeDelegationRes::Error(err) => Err(err),
TypeRelativeDelegationRes::InherentFnNotFound => {
Err(tcx.dcx().emit_err(FailedToResolveDelegationToInherentImpl { span }))
}
TypeRelativeDelegationRes::Ambig(_) => {
Err(tcx.dcx().emit_err(AmbiguousDelegationToInherentImpl { span }))
}
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_ast_lowering/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,13 @@ pub(crate) struct AmbiguousDelegationToInherentImpl {
pub span: Span,
}

#[derive(Diagnostic)]
#[diag("failed to resolve delegation to inherent impl")]
pub(crate) struct FailedToResolveDelegationToInherentImpl {
#[primary_span]
pub span: Span,
}

#[derive(Diagnostic)]
#[diag("delegation to inherent impl must contain parent generics")]
pub(crate) struct DelegationToInherentImplMustContainParentGenerics {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/middle/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ pub enum TypeRelativeDelegationRes {
Ok(DefId),
Ambig(ErrorGuaranteed),
Error(ErrorGuaranteed),
InherentFnNotFound,
}

#[derive(Debug, StableHash)]
Expand Down
18 changes: 18 additions & 0 deletions tests/ui/delegation/delegation-inherent-impl-issue-162774.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#![feature(fn_delegation)]

trait Trait {
fn foo(&self) {}
}

struct F;
impl Trait for F {}

struct S(F);

impl S {
reuse Trait::foo { self.0 }
reuse Self::foo::<> as bar { self }
//~^ ERROR failed to resolve delegation to inherent impl
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: failed to resolve delegation to inherent impl
--> $DIR/delegation-inherent-impl-issue-162774.rs:14:17
|
LL | reuse Self::foo::<> as bar { self }
| ^^^

error: aborting due to 1 previous error

Loading