Recursive delegation improvements
This commit is contained in:
parent
efa32de15b
commit
3f477dd710
11 changed files with 522 additions and 142 deletions
|
|
@ -48,11 +48,12 @@ use rustc_data_structures::fx::FxHashSet;
|
|||
use rustc_errors::ErrorGuaranteed;
|
||||
use rustc_hir::Target;
|
||||
use rustc_hir::attrs::{AttributeKind, InlineAttr};
|
||||
use rustc_hir::def_id::DefId;
|
||||
use rustc_hir::def_id::{DefId, LocalDefId};
|
||||
use rustc_middle::span_bug;
|
||||
use rustc_middle::ty::{Asyncness, DelegationFnSigAttrs, ResolverAstLowering};
|
||||
use rustc_middle::ty::{Asyncness, DelegationAttrs, DelegationFnSigAttrs, ResolverAstLowering};
|
||||
use rustc_span::symbol::kw;
|
||||
use rustc_span::{DUMMY_SP, Ident, Span, Symbol};
|
||||
use smallvec::SmallVec;
|
||||
use {rustc_ast as ast, rustc_hir as hir};
|
||||
|
||||
use super::{GenericArgsMode, ImplTraitContext, LoweringContext, ParamMode};
|
||||
|
|
@ -66,24 +67,24 @@ pub(crate) struct DelegationResults<'hir> {
|
|||
pub generics: &'hir hir::Generics<'hir>,
|
||||
}
|
||||
|
||||
struct AttributeAdditionInfo {
|
||||
struct AttrAdditionInfo {
|
||||
pub equals: fn(&hir::Attribute) -> bool,
|
||||
pub kind: AttributeAdditionKind,
|
||||
pub kind: AttrAdditionKind,
|
||||
}
|
||||
|
||||
enum AttributeAdditionKind {
|
||||
enum AttrAdditionKind {
|
||||
Default { factory: fn(Span) -> hir::Attribute },
|
||||
Inherit { flag: DelegationFnSigAttrs, factory: fn(Span, &hir::Attribute) -> hir::Attribute },
|
||||
}
|
||||
|
||||
const PARENT_ID: hir::ItemLocalId = hir::ItemLocalId::ZERO;
|
||||
|
||||
static ATTRIBUTES_ADDITIONS: &[AttributeAdditionInfo] = &[
|
||||
AttributeAdditionInfo {
|
||||
static ATTRS_ADDITIONS: &[AttrAdditionInfo] = &[
|
||||
AttrAdditionInfo {
|
||||
equals: |a| matches!(a, hir::Attribute::Parsed(AttributeKind::MustUse { .. })),
|
||||
kind: AttributeAdditionKind::Inherit {
|
||||
factory: |span, original_attribute| {
|
||||
let reason = match original_attribute {
|
||||
kind: AttrAdditionKind::Inherit {
|
||||
factory: |span, original_attr| {
|
||||
let reason = match original_attr {
|
||||
hir::Attribute::Parsed(AttributeKind::MustUse { reason, .. }) => *reason,
|
||||
_ => None,
|
||||
};
|
||||
|
|
@ -93,14 +94,41 @@ static ATTRIBUTES_ADDITIONS: &[AttributeAdditionInfo] = &[
|
|||
flag: DelegationFnSigAttrs::MUST_USE,
|
||||
},
|
||||
},
|
||||
AttributeAdditionInfo {
|
||||
AttrAdditionInfo {
|
||||
equals: |a| matches!(a, hir::Attribute::Parsed(AttributeKind::Inline(..))),
|
||||
kind: AttributeAdditionKind::Default {
|
||||
kind: AttrAdditionKind::Default {
|
||||
factory: |span| hir::Attribute::Parsed(AttributeKind::Inline(InlineAttr::Hint, span)),
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
type DelegationIdsVec = SmallVec<[DefId; 1]>;
|
||||
|
||||
// As delegations can now refer to another delegation, we have a delegation path
|
||||
// of the following type: reuse (current delegation) <- reuse (delegee_id) <- ... <- reuse <- function (root_function_id).
|
||||
// In its most basic and widely used form: reuse (current delegation) <- function (delegee_id, root_function_id)
|
||||
struct DelegationIds {
|
||||
path: DelegationIdsVec,
|
||||
}
|
||||
|
||||
impl DelegationIds {
|
||||
fn new(path: DelegationIdsVec) -> Self {
|
||||
assert!(!path.is_empty());
|
||||
Self { path }
|
||||
}
|
||||
|
||||
// Id of the first function in (non)local crate that is being reused
|
||||
fn root_function_id(&self) -> DefId {
|
||||
*self.path.last().expect("Ids vector can't be empty")
|
||||
}
|
||||
|
||||
// Id of the first definition which is being reused,
|
||||
// can be either function, in this case `root_id == delegee_id`, or other delegation
|
||||
fn delegee_id(&self) -> DefId {
|
||||
*self.path.first().expect("Ids vector can't be empty")
|
||||
}
|
||||
}
|
||||
|
||||
impl<'hir> LoweringContext<'_, 'hir> {
|
||||
fn is_method(&self, def_id: DefId, span: Span) -> bool {
|
||||
match self.tcx.def_kind(def_id) {
|
||||
|
|
@ -124,19 +152,36 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
|||
) -> DelegationResults<'hir> {
|
||||
let span = self.lower_span(delegation.path.segments.last().unwrap().ident.span);
|
||||
|
||||
let sig_id = self.get_delegation_sig_id(
|
||||
self.resolver.delegation_sig_resolution_nodes[&self.local_def_id(item_id)],
|
||||
let ids = self.get_delegation_ids(
|
||||
self.resolver.delegation_infos[&self.local_def_id(item_id)].resolution_node,
|
||||
span,
|
||||
);
|
||||
|
||||
match sig_id {
|
||||
Ok(sig_id) => {
|
||||
self.add_attributes_if_needed(span, sig_id);
|
||||
match ids {
|
||||
Ok(ids) => {
|
||||
self.add_attrs_if_needed(span, &ids);
|
||||
|
||||
let delegee_id = ids.delegee_id();
|
||||
let root_function_id = ids.root_function_id();
|
||||
|
||||
// `is_method` is used to choose the name of the first parameter (`self` or `arg0`),
|
||||
// if the original function is not a method (without `self`), then it can not be added
|
||||
// during chain of reuses, so we use `root_function_id` here
|
||||
let is_method = self.is_method(root_function_id, span);
|
||||
|
||||
// Here we use `root_function_id` as we can not get params information out of potential delegation reuse,
|
||||
// we need a function to extract this information
|
||||
let (param_count, c_variadic) = self.param_count(root_function_id);
|
||||
|
||||
// Here we use `delegee_id`, as this id will then be used to calculate parent for generics
|
||||
// inheritance, and we want this id to point on a delegee, not on the original
|
||||
// function (see https://github.com/rust-lang/rust/issues/150152#issuecomment-3674834654)
|
||||
let decl = self.lower_delegation_decl(delegee_id, param_count, c_variadic, span);
|
||||
|
||||
// Here we pass `root_function_id` as we want to inherit signature (including consts, async)
|
||||
// from the root function that started delegation
|
||||
let sig = self.lower_delegation_sig(root_function_id, decl, span);
|
||||
|
||||
let is_method = self.is_method(sig_id, span);
|
||||
let (param_count, c_variadic) = self.param_count(sig_id);
|
||||
let decl = self.lower_delegation_decl(sig_id, param_count, c_variadic, span);
|
||||
let sig = self.lower_delegation_sig(sig_id, decl, span);
|
||||
let body_id = self.lower_delegation_body(delegation, is_method, param_count, span);
|
||||
let ident = self.lower_ident(delegation.ident);
|
||||
let generics = self.lower_delegation_generics(span);
|
||||
|
|
@ -146,36 +191,36 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
|||
}
|
||||
}
|
||||
|
||||
fn add_attributes_if_needed(&mut self, span: Span, sig_id: DefId) {
|
||||
let new_attributes = self.create_new_attributes(
|
||||
ATTRIBUTES_ADDITIONS,
|
||||
span,
|
||||
sig_id,
|
||||
self.attrs.get(&PARENT_ID),
|
||||
);
|
||||
fn add_attrs_if_needed(&mut self, span: Span, ids: &DelegationIds) {
|
||||
let new_attrs =
|
||||
self.create_new_attrs(ATTRS_ADDITIONS, span, ids, self.attrs.get(&PARENT_ID));
|
||||
|
||||
if new_attributes.is_empty() {
|
||||
if new_attrs.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
let new_arena_allocated_attributes = match self.attrs.get(&PARENT_ID) {
|
||||
let new_arena_allocated_attrs = match self.attrs.get(&PARENT_ID) {
|
||||
Some(existing_attrs) => self.arena.alloc_from_iter(
|
||||
existing_attrs.iter().map(|a| a.clone()).chain(new_attributes.into_iter()),
|
||||
existing_attrs.iter().map(|a| a.clone()).chain(new_attrs.into_iter()),
|
||||
),
|
||||
None => self.arena.alloc_from_iter(new_attributes.into_iter()),
|
||||
None => self.arena.alloc_from_iter(new_attrs.into_iter()),
|
||||
};
|
||||
|
||||
self.attrs.insert(PARENT_ID, new_arena_allocated_attributes);
|
||||
self.attrs.insert(PARENT_ID, new_arena_allocated_attrs);
|
||||
}
|
||||
|
||||
fn create_new_attributes(
|
||||
fn create_new_attrs(
|
||||
&self,
|
||||
candidate_additions: &[AttributeAdditionInfo],
|
||||
candidate_additions: &[AttrAdditionInfo],
|
||||
span: Span,
|
||||
sig_id: DefId,
|
||||
ids: &DelegationIds,
|
||||
existing_attrs: Option<&&[hir::Attribute]>,
|
||||
) -> Vec<hir::Attribute> {
|
||||
let local_original_attributes = self.parse_local_original_attributes(sig_id);
|
||||
let defs_orig_attrs = ids
|
||||
.path
|
||||
.iter()
|
||||
.map(|def_id| (*def_id, self.parse_local_original_attrs(*def_id)))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
candidate_additions
|
||||
.iter()
|
||||
|
|
@ -189,65 +234,81 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
|||
}
|
||||
|
||||
match addition_info.kind {
|
||||
AttributeAdditionKind::Default { factory } => Some(factory(span)),
|
||||
AttributeAdditionKind::Inherit { flag, factory } => {
|
||||
let original_attribute = match sig_id.as_local() {
|
||||
Some(local_id) => self
|
||||
.resolver
|
||||
.delegation_fn_sigs
|
||||
.get(&local_id)
|
||||
.is_some_and(|sig| sig.attrs_flags.contains(flag))
|
||||
.then(|| {
|
||||
local_original_attributes
|
||||
.as_ref()
|
||||
.map(|attrs| {
|
||||
attrs
|
||||
.iter()
|
||||
.find(|base_attr| (addition_info.equals)(base_attr))
|
||||
})
|
||||
.flatten()
|
||||
})
|
||||
.flatten(),
|
||||
None => self
|
||||
.tcx
|
||||
.get_all_attrs(sig_id)
|
||||
.iter()
|
||||
.find(|base_attr| (addition_info.equals)(base_attr)),
|
||||
};
|
||||
AttrAdditionKind::Default { factory } => Some(factory(span)),
|
||||
AttrAdditionKind::Inherit { flag, factory } => {
|
||||
for (def_id, orig_attrs) in &defs_orig_attrs {
|
||||
let original_attr = match def_id.as_local() {
|
||||
Some(local_id) => self
|
||||
.get_attrs(local_id)
|
||||
.flags
|
||||
.contains(flag)
|
||||
.then(|| {
|
||||
orig_attrs
|
||||
.as_ref()
|
||||
.map(|attrs| {
|
||||
attrs.iter().find(|base_attr| {
|
||||
(addition_info.equals)(base_attr)
|
||||
})
|
||||
})
|
||||
.flatten()
|
||||
})
|
||||
.flatten(),
|
||||
None => self
|
||||
.tcx
|
||||
.get_all_attrs(*def_id)
|
||||
.iter()
|
||||
.find(|base_attr| (addition_info.equals)(base_attr)),
|
||||
};
|
||||
|
||||
original_attribute.map(|a| factory(span, a))
|
||||
if let Some(original_attr) = original_attr {
|
||||
return Some(factory(span, original_attr));
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
}
|
||||
|
||||
fn parse_local_original_attributes(&self, sig_id: DefId) -> Option<Vec<hir::Attribute>> {
|
||||
if let Some(local_id) = sig_id.as_local()
|
||||
&& let Some(info) = self.resolver.delegation_fn_sigs.get(&local_id)
|
||||
&& !info.to_inherit_attrs.is_empty()
|
||||
{
|
||||
Some(AttributeParser::parse_limited_all(
|
||||
self.tcx.sess,
|
||||
info.to_inherit_attrs.as_slice(),
|
||||
None,
|
||||
Target::Fn,
|
||||
DUMMY_SP,
|
||||
DUMMY_NODE_ID,
|
||||
Some(self.tcx.features()),
|
||||
ShouldEmit::Nothing,
|
||||
))
|
||||
fn parse_local_original_attrs(&self, def_id: DefId) -> Option<Vec<hir::Attribute>> {
|
||||
if let Some(local_id) = def_id.as_local() {
|
||||
let attrs = &self.get_attrs(local_id).to_inherit;
|
||||
|
||||
if !attrs.is_empty() {
|
||||
return Some(AttributeParser::parse_limited_all(
|
||||
self.tcx.sess,
|
||||
attrs,
|
||||
None,
|
||||
Target::Fn,
|
||||
DUMMY_SP,
|
||||
DUMMY_NODE_ID,
|
||||
Some(self.tcx.features()),
|
||||
ShouldEmit::Nothing,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
fn get_attrs(&self, local_id: LocalDefId) -> &DelegationAttrs {
|
||||
// local_id can correspond either to a function or other delegation
|
||||
if let Some(fn_sig) = self.resolver.delegation_fn_sigs.get(&local_id) {
|
||||
&fn_sig.attrs
|
||||
} else {
|
||||
None
|
||||
&self.resolver.delegation_infos[&local_id].attrs
|
||||
}
|
||||
}
|
||||
|
||||
fn get_delegation_sig_id(
|
||||
fn get_delegation_ids(
|
||||
&self,
|
||||
mut node_id: NodeId,
|
||||
span: Span,
|
||||
) -> Result<DefId, ErrorGuaranteed> {
|
||||
) -> Result<DelegationIds, ErrorGuaranteed> {
|
||||
let mut visited: FxHashSet<NodeId> = Default::default();
|
||||
let mut path: DelegationIdsVec = Default::default();
|
||||
|
||||
loop {
|
||||
visited.insert(node_id);
|
||||
|
|
@ -262,14 +323,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
|||
));
|
||||
};
|
||||
|
||||
path.push(def_id);
|
||||
|
||||
// If def_id is in local crate and it corresponds to another delegation
|
||||
// it means that we refer to another delegation as a callee, so in order to obtain
|
||||
// a signature DefId we obtain NodeId of the callee delegation and try to get signature from it.
|
||||
if let Some(local_id) = def_id.as_local()
|
||||
&& let Some(next_node_id) =
|
||||
self.resolver.delegation_sig_resolution_nodes.get(&local_id)
|
||||
&& let Some(delegation_info) = self.resolver.delegation_infos.get(&local_id)
|
||||
{
|
||||
node_id = *next_node_id;
|
||||
node_id = delegation_info.resolution_node;
|
||||
if visited.contains(&node_id) {
|
||||
// We encountered a cycle in the resolution, or delegation callee refers to non-existent
|
||||
// entity, in this case emit an error.
|
||||
|
|
@ -279,7 +341,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
|||
});
|
||||
}
|
||||
} else {
|
||||
return Ok(def_id);
|
||||
return Ok(DelegationIds::new(path));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -299,14 +361,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
|||
}
|
||||
|
||||
// Function parameter count, including C variadic `...` if present.
|
||||
fn param_count(&self, sig_id: DefId) -> (usize, bool /*c_variadic*/) {
|
||||
if let Some(local_sig_id) = sig_id.as_local() {
|
||||
fn param_count(&self, def_id: DefId) -> (usize, bool /*c_variadic*/) {
|
||||
if let Some(local_sig_id) = def_id.as_local() {
|
||||
match self.resolver.delegation_fn_sigs.get(&local_sig_id) {
|
||||
Some(sig) => (sig.param_count, sig.c_variadic),
|
||||
None => (0, false),
|
||||
}
|
||||
} else {
|
||||
let sig = self.tcx.fn_sig(sig_id).skip_binder().skip_binder();
|
||||
let sig = self.tcx.fn_sig(def_id).skip_binder().skip_binder();
|
||||
(sig.inputs().len() + usize::from(sig.c_variadic), sig.c_variadic)
|
||||
}
|
||||
}
|
||||
|
|
@ -356,7 +418,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
|||
// We are not forwarding the attributes, as the delegation fn sigs are collected on the ast,
|
||||
// and here we need the hir attributes.
|
||||
let default_safety =
|
||||
if sig.attrs_flags.contains(DelegationFnSigAttrs::TARGET_FEATURE)
|
||||
if sig.attrs.flags.contains(DelegationFnSigAttrs::TARGET_FEATURE)
|
||||
|| self.tcx.def_kind(parent) == DefKind::ForeignMod
|
||||
{
|
||||
hir::Safety::Unsafe
|
||||
|
|
|
|||
|
|
@ -220,9 +220,8 @@ pub struct ResolverAstLowering {
|
|||
|
||||
/// Information about functions signatures for delegation items expansion
|
||||
pub delegation_fn_sigs: LocalDefIdMap<DelegationFnSig>,
|
||||
// NodeIds (either delegation.id or item_id in case of a trait impl) for signature resolution,
|
||||
// for details see https://github.com/rust-lang/rust/issues/118212#issuecomment-2160686914
|
||||
pub delegation_sig_resolution_nodes: LocalDefIdMap<ast::NodeId>,
|
||||
// Information about delegations which is used when handling recursive delegations
|
||||
pub delegation_infos: LocalDefIdMap<DelegationInfo>,
|
||||
}
|
||||
|
||||
bitflags::bitflags! {
|
||||
|
|
@ -235,14 +234,27 @@ bitflags::bitflags! {
|
|||
|
||||
pub const DELEGATION_INHERIT_ATTRS_START: DelegationFnSigAttrs = DelegationFnSigAttrs::MUST_USE;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct DelegationInfo {
|
||||
// NodeId (either delegation.id or item_id in case of a trait impl) for signature resolution,
|
||||
// for details see https://github.com/rust-lang/rust/issues/118212#issuecomment-2160686914
|
||||
pub resolution_node: ast::NodeId,
|
||||
pub attrs: DelegationAttrs,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct DelegationAttrs {
|
||||
pub flags: DelegationFnSigAttrs,
|
||||
pub to_inherit: AttrVec,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct DelegationFnSig {
|
||||
pub header: ast::FnHeader,
|
||||
pub param_count: usize,
|
||||
pub has_self: bool,
|
||||
pub c_variadic: bool,
|
||||
pub attrs_flags: DelegationFnSigAttrs,
|
||||
pub to_inherit_attrs: AttrVec,
|
||||
pub attrs: DelegationAttrs,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, HashStable)]
|
||||
|
|
|
|||
|
|
@ -29,7 +29,8 @@ use rustc_hir::def_id::{CRATE_DEF_ID, DefId, LOCAL_CRATE, LocalDefId};
|
|||
use rustc_hir::{MissingLifetimeKind, PrimTy, TraitCandidate};
|
||||
use rustc_middle::middle::resolve_bound_vars::Set1;
|
||||
use rustc_middle::ty::{
|
||||
AssocTag, DELEGATION_INHERIT_ATTRS_START, DelegationFnSig, DelegationFnSigAttrs, Visibility,
|
||||
AssocTag, DELEGATION_INHERIT_ATTRS_START, DelegationAttrs, DelegationFnSig,
|
||||
DelegationFnSigAttrs, DelegationInfo, Visibility,
|
||||
};
|
||||
use rustc_middle::{bug, span_bug};
|
||||
use rustc_session::config::{CrateType, ResolveDocLinks};
|
||||
|
|
@ -2928,7 +2929,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
|
|||
item.id,
|
||||
LifetimeBinderKind::Function,
|
||||
span,
|
||||
|this| this.resolve_delegation(delegation, item.id, false),
|
||||
|this| this.resolve_delegation(delegation, item.id, false, &item.attrs),
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -3257,7 +3258,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
|
|||
item.id,
|
||||
LifetimeBinderKind::Function,
|
||||
delegation.path.segments.last().unwrap().ident.span,
|
||||
|this| this.resolve_delegation(delegation, item.id, false),
|
||||
|this| this.resolve_delegation(delegation, item.id, false, &item.attrs),
|
||||
);
|
||||
}
|
||||
AssocItemKind::Type(box TyAlias { generics, .. }) => self
|
||||
|
|
@ -3386,7 +3387,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
|
|||
debug!("resolve_implementation with_self_rib_ns(ValueNS, ...)");
|
||||
let mut seen_trait_items = Default::default();
|
||||
for item in impl_items {
|
||||
this.resolve_impl_item(&**item, &mut seen_trait_items, trait_id);
|
||||
this.resolve_impl_item(&**item, &mut seen_trait_items, trait_id, of_trait.is_some());
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
@ -3405,6 +3406,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
|
|||
item: &'ast AssocItem,
|
||||
seen_trait_items: &mut FxHashMap<DefId, Span>,
|
||||
trait_id: Option<DefId>,
|
||||
is_in_trait_impl: bool,
|
||||
) {
|
||||
use crate::ResolutionError::*;
|
||||
self.resolve_doc_links(&item.attrs, MaybeExported::ImplItem(trait_id.ok_or(&item.vis)));
|
||||
|
|
@ -3550,7 +3552,9 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
|
|||
|i, s, c| MethodNotMemberOfTrait(i, s, c),
|
||||
);
|
||||
|
||||
this.resolve_delegation(delegation, item.id, trait_id.is_some());
|
||||
// Here we don't use `trait_id`, as we can process unresolved trait, however
|
||||
// in this case we are still in a trait impl, https://github.com/rust-lang/rust/issues/150152
|
||||
this.resolve_delegation(delegation, item.id, is_in_trait_impl, &item.attrs);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
|
@ -3704,6 +3708,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
|
|||
delegation: &'ast Delegation,
|
||||
item_id: NodeId,
|
||||
is_in_trait_impl: bool,
|
||||
attrs: &[Attribute],
|
||||
) {
|
||||
self.smart_resolve_path(
|
||||
delegation.id,
|
||||
|
|
@ -3718,9 +3723,12 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
|
|||
|
||||
self.visit_path(&delegation.path);
|
||||
|
||||
self.r.delegation_sig_resolution_nodes.insert(
|
||||
self.r.delegation_infos.insert(
|
||||
self.r.local_def_id(item_id),
|
||||
if is_in_trait_impl { item_id } else { delegation.id },
|
||||
DelegationInfo {
|
||||
attrs: create_delegation_attrs(attrs),
|
||||
resolution_node: if is_in_trait_impl { item_id } else { delegation.id },
|
||||
},
|
||||
);
|
||||
|
||||
let Some(body) = &delegation.body else { return };
|
||||
|
|
@ -5334,39 +5342,43 @@ impl ItemInfoCollector<'_, '_, '_> {
|
|||
id: NodeId,
|
||||
attrs: &[Attribute],
|
||||
) {
|
||||
static NAMES_TO_FLAGS: &[(Symbol, DelegationFnSigAttrs)] = &[
|
||||
(sym::target_feature, DelegationFnSigAttrs::TARGET_FEATURE),
|
||||
(sym::must_use, DelegationFnSigAttrs::MUST_USE),
|
||||
];
|
||||
self.r.delegation_fn_sigs.insert(
|
||||
self.r.local_def_id(id),
|
||||
DelegationFnSig {
|
||||
header,
|
||||
param_count: decl.inputs.len(),
|
||||
has_self: decl.has_self(),
|
||||
c_variadic: decl.c_variadic(),
|
||||
attrs: create_delegation_attrs(attrs),
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
let mut to_inherit_attrs = AttrVec::new();
|
||||
let mut attrs_flags = DelegationFnSigAttrs::empty();
|
||||
fn create_delegation_attrs(attrs: &[Attribute]) -> DelegationAttrs {
|
||||
static NAMES_TO_FLAGS: &[(Symbol, DelegationFnSigAttrs)] = &[
|
||||
(sym::target_feature, DelegationFnSigAttrs::TARGET_FEATURE),
|
||||
(sym::must_use, DelegationFnSigAttrs::MUST_USE),
|
||||
];
|
||||
|
||||
'attrs_loop: for attr in attrs {
|
||||
for &(name, flag) in NAMES_TO_FLAGS {
|
||||
if attr.has_name(name) {
|
||||
attrs_flags.set(flag, true);
|
||||
let mut to_inherit_attrs = AttrVec::new();
|
||||
let mut flags = DelegationFnSigAttrs::empty();
|
||||
|
||||
if flag.bits() >= DELEGATION_INHERIT_ATTRS_START.bits() {
|
||||
to_inherit_attrs.push(attr.clone());
|
||||
}
|
||||
'attrs_loop: for attr in attrs {
|
||||
for &(name, flag) in NAMES_TO_FLAGS {
|
||||
if attr.has_name(name) {
|
||||
flags.set(flag, true);
|
||||
|
||||
continue 'attrs_loop;
|
||||
if flag.bits() >= DELEGATION_INHERIT_ATTRS_START.bits() {
|
||||
to_inherit_attrs.push(attr.clone());
|
||||
}
|
||||
|
||||
continue 'attrs_loop;
|
||||
}
|
||||
}
|
||||
|
||||
let sig = DelegationFnSig {
|
||||
header,
|
||||
param_count: decl.inputs.len(),
|
||||
has_self: decl.has_self(),
|
||||
c_variadic: decl.c_variadic(),
|
||||
attrs_flags,
|
||||
to_inherit_attrs,
|
||||
};
|
||||
|
||||
self.r.delegation_fn_sigs.insert(self.r.local_def_id(id), sig);
|
||||
}
|
||||
|
||||
DelegationAttrs { flags, to_inherit: to_inherit_attrs }
|
||||
}
|
||||
|
||||
impl<'ast> Visitor<'ast> for ItemInfoCollector<'_, '_, '_> {
|
||||
|
|
|
|||
|
|
@ -69,8 +69,8 @@ use rustc_middle::middle::privacy::EffectiveVisibilities;
|
|||
use rustc_middle::query::Providers;
|
||||
use rustc_middle::span_bug;
|
||||
use rustc_middle::ty::{
|
||||
self, DelegationFnSig, Feed, MainDefinition, RegisteredTools, ResolverAstLowering,
|
||||
ResolverGlobalCtxt, TyCtxt, TyCtxtFeed, Visibility,
|
||||
self, DelegationFnSig, DelegationInfo, Feed, MainDefinition, RegisteredTools,
|
||||
ResolverAstLowering, ResolverGlobalCtxt, TyCtxt, TyCtxtFeed, Visibility,
|
||||
};
|
||||
use rustc_query_system::ich::StableHashingContext;
|
||||
use rustc_session::config::CrateType;
|
||||
|
|
@ -1283,7 +1283,7 @@ pub struct Resolver<'ra, 'tcx> {
|
|||
/// Amount of lifetime parameters for each item in the crate.
|
||||
item_generics_num_lifetimes: FxHashMap<LocalDefId, usize>,
|
||||
delegation_fn_sigs: LocalDefIdMap<DelegationFnSig>,
|
||||
delegation_sig_resolution_nodes: LocalDefIdMap<NodeId>,
|
||||
delegation_infos: LocalDefIdMap<DelegationInfo>,
|
||||
|
||||
main_def: Option<MainDefinition> = None,
|
||||
trait_impls: FxIndexMap<DefId, Vec<LocalDefId>>,
|
||||
|
|
@ -1701,7 +1701,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
|
|||
current_crate_outer_attr_insert_span,
|
||||
mods_with_parse_errors: Default::default(),
|
||||
impl_trait_names: Default::default(),
|
||||
delegation_sig_resolution_nodes: Default::default(),
|
||||
delegation_infos: Default::default(),
|
||||
..
|
||||
};
|
||||
|
||||
|
|
@ -1829,7 +1829,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
|
|||
lifetime_elision_allowed: self.lifetime_elision_allowed,
|
||||
lint_buffer: Steal::new(self.lint_buffer),
|
||||
delegation_fn_sigs: self.delegation_fn_sigs,
|
||||
delegation_sig_resolution_nodes: self.delegation_sig_resolution_nodes,
|
||||
delegation_infos: self.delegation_infos,
|
||||
};
|
||||
ResolverOutputs { global_ctxt, ast_lowering }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -57,5 +57,69 @@ const fn const_fn_extern() -> _ { to_reuse_functions::const_fn_extern() }
|
|||
#[attr = Inline(Hint)]
|
||||
async fn async_fn_extern() -> _ { to_reuse_functions::async_fn_extern() }
|
||||
|
||||
mod recursive {
|
||||
// Check that `baz` inherit attribute from `foo`
|
||||
mod first {
|
||||
fn bar() { }
|
||||
#[attr = MustUse {reason: "some reason"}]
|
||||
#[attr = Inline(Hint)]
|
||||
fn foo() -> _ { bar() }
|
||||
#[attr = MustUse {reason: "some reason"}]
|
||||
#[attr = Inline(Hint)]
|
||||
fn baz() -> _ { foo() }
|
||||
}
|
||||
|
||||
// Check that `baz` inherit attribute from `bar`
|
||||
mod second {
|
||||
#[attr = MustUse {reason: "some reason"}]
|
||||
fn bar() { }
|
||||
|
||||
#[attr = MustUse {reason: "some reason"}]
|
||||
#[attr = Inline(Hint)]
|
||||
fn foo() -> _ { bar() }
|
||||
#[attr = MustUse {reason: "some reason"}]
|
||||
#[attr = Inline(Hint)]
|
||||
fn baz() -> _ { foo() }
|
||||
}
|
||||
|
||||
// Check that `foo5` don't inherit attribute from `bar`
|
||||
// and inherit attribute from foo4, check that foo1, foo2 and foo3
|
||||
// inherit attribute from bar
|
||||
mod third {
|
||||
#[attr = MustUse {reason: "some reason"}]
|
||||
fn bar() { }
|
||||
#[attr = MustUse {reason: "some reason"}]
|
||||
#[attr = Inline(Hint)]
|
||||
fn foo1() -> _ { bar() }
|
||||
#[attr = MustUse {reason: "some reason"}]
|
||||
#[attr = Inline(Hint)]
|
||||
fn foo2() -> _ { foo1() }
|
||||
#[attr = MustUse {reason: "some reason"}]
|
||||
#[attr = Inline(Hint)]
|
||||
fn foo3() -> _ { foo2() }
|
||||
#[attr = MustUse {reason: "foo4"}]
|
||||
#[attr = Inline(Hint)]
|
||||
fn foo4() -> _ { foo3() }
|
||||
#[attr = MustUse {reason: "foo4"}]
|
||||
#[attr = Inline(Hint)]
|
||||
fn foo5() -> _ { foo4() }
|
||||
}
|
||||
|
||||
mod fourth {
|
||||
trait T {
|
||||
fn foo(&self, x: usize) -> usize { x + 1 }
|
||||
}
|
||||
|
||||
struct X;
|
||||
impl T for X { }
|
||||
|
||||
#[attr = MustUse {reason: "some reason"}]
|
||||
#[attr = Inline(Hint)]
|
||||
fn foo(self: _, arg1: _) -> _ { <X as T>::foo(self + 1, arg1) }
|
||||
#[attr = MustUse {reason: "some reason"}]
|
||||
#[attr = Inline(Hint)]
|
||||
fn bar(self: _, arg1: _) -> _ { foo(self + 1, arg1) }
|
||||
}
|
||||
}
|
||||
|
||||
fn main() { }
|
||||
|
|
|
|||
|
|
@ -52,5 +52,50 @@ reuse to_reuse_functions::const_fn_extern;
|
|||
#[must_use = "some reason"]
|
||||
reuse to_reuse_functions::async_fn_extern;
|
||||
|
||||
mod recursive {
|
||||
// Check that `baz` inherit attribute from `foo`
|
||||
mod first {
|
||||
fn bar() {}
|
||||
#[must_use = "some reason"]
|
||||
reuse bar as foo;
|
||||
reuse foo as baz;
|
||||
}
|
||||
|
||||
// Check that `baz` inherit attribute from `bar`
|
||||
mod second {
|
||||
#[must_use = "some reason"]
|
||||
fn bar() {}
|
||||
|
||||
reuse bar as foo;
|
||||
reuse foo as baz;
|
||||
}
|
||||
|
||||
// Check that `foo5` don't inherit attribute from `bar`
|
||||
// and inherit attribute from foo4, check that foo1, foo2 and foo3
|
||||
// inherit attribute from bar
|
||||
mod third {
|
||||
#[must_use = "some reason"]
|
||||
fn bar() {}
|
||||
reuse bar as foo1;
|
||||
reuse foo1 as foo2;
|
||||
reuse foo2 as foo3;
|
||||
#[must_use = "foo4"]
|
||||
reuse foo3 as foo4;
|
||||
reuse foo4 as foo5;
|
||||
}
|
||||
|
||||
mod fourth {
|
||||
trait T {
|
||||
fn foo(&self, x: usize) -> usize { x + 1 }
|
||||
}
|
||||
|
||||
struct X;
|
||||
impl T for X {}
|
||||
|
||||
#[must_use = "some reason"]
|
||||
reuse <X as T>::foo { self + 1 }
|
||||
reuse foo as bar { self + 1 }
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
//@ pretty-compare-only
|
||||
//@ pretty-mode:hir
|
||||
//@ pp-exact:delegation_inline_attribute.pp
|
||||
//@ pp-exact:delegation-inline-attribute.pp
|
||||
|
||||
#![allow(incomplete_features)]
|
||||
#![feature(fn_delegation)]
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
//@ pretty-compare-only
|
||||
//@ pretty-mode:hir
|
||||
//@ pp-exact:delegation_inline_attribute.pp
|
||||
//@ pp-exact:delegation-inline-attribute.pp
|
||||
|
||||
#![allow(incomplete_features)]
|
||||
#![feature(fn_delegation)]
|
||||
31
tests/ui/delegation/recursive-delegation-ice-150152.rs
Normal file
31
tests/ui/delegation/recursive-delegation-ice-150152.rs
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
#![feature(fn_delegation)]
|
||||
#![allow(incomplete_features)]
|
||||
|
||||
mod first_example {
|
||||
mod to_reuse { pub fn foo() {} }
|
||||
struct S< S >;
|
||||
//~^ ERROR type parameter `S` is never used
|
||||
|
||||
impl Item for S<S> {
|
||||
//~^ ERROR cannot find trait `Item` in this scope
|
||||
//~| ERROR missing generics for struct `S`
|
||||
reuse to_reuse::foo;
|
||||
}
|
||||
}
|
||||
|
||||
mod second_example {
|
||||
trait Trait {
|
||||
reuse to_reuse::foo;
|
||||
//~^ ERROR function `foo` is private
|
||||
}
|
||||
mod to_reuse {
|
||||
fn foo() {}
|
||||
}
|
||||
impl Trait for S {
|
||||
//~^ ERROR cannot find type `S` in this scope
|
||||
reuse foo;
|
||||
//~^ ERROR cannot find function `foo` in this scope
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
74
tests/ui/delegation/recursive-delegation-ice-150152.stderr
Normal file
74
tests/ui/delegation/recursive-delegation-ice-150152.stderr
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
error[E0405]: cannot find trait `Item` in this scope
|
||||
--> $DIR/recursive-delegation-ice-150152.rs:9:10
|
||||
|
|
||||
LL | impl Item for S<S> {
|
||||
| ^^^^ not found in this scope
|
||||
|
||||
error[E0425]: cannot find type `S` in this scope
|
||||
--> $DIR/recursive-delegation-ice-150152.rs:24:20
|
||||
|
|
||||
LL | impl Trait for S {
|
||||
| ^ not found in this scope
|
||||
|
|
||||
note: struct `first_example::S` exists but is inaccessible
|
||||
--> $DIR/recursive-delegation-ice-150152.rs:6:5
|
||||
|
|
||||
LL | struct S< S >;
|
||||
| ^^^^^^^^^^^^^^ not accessible
|
||||
|
||||
error[E0425]: cannot find function `foo` in this scope
|
||||
--> $DIR/recursive-delegation-ice-150152.rs:26:15
|
||||
|
|
||||
LL | reuse foo;
|
||||
| ^^^ not found in this scope
|
||||
|
|
||||
note: these functions exist but are inaccessible
|
||||
--> $DIR/recursive-delegation-ice-150152.rs:5:20
|
||||
|
|
||||
LL | mod to_reuse { pub fn foo() {} }
|
||||
| ^^^^^^^^^^^^ `first_example::to_reuse::foo`: not accessible
|
||||
...
|
||||
LL | fn foo() {}
|
||||
| ^^^^^^^^ `second_example::to_reuse::foo`: not accessible
|
||||
|
||||
error[E0603]: function `foo` is private
|
||||
--> $DIR/recursive-delegation-ice-150152.rs:18:25
|
||||
|
|
||||
LL | reuse to_reuse::foo;
|
||||
| ^^^ private function
|
||||
|
|
||||
note: the function `foo` is defined here
|
||||
--> $DIR/recursive-delegation-ice-150152.rs:22:9
|
||||
|
|
||||
LL | fn foo() {}
|
||||
| ^^^^^^^^
|
||||
|
||||
error[E0392]: type parameter `S` is never used
|
||||
--> $DIR/recursive-delegation-ice-150152.rs:6:15
|
||||
|
|
||||
LL | struct S< S >;
|
||||
| ^ unused type parameter
|
||||
|
|
||||
= help: consider removing `S`, referring to it in a field, or using a marker such as `PhantomData`
|
||||
= help: if you intended `S` to be a const parameter, use `const S: /* Type */` instead
|
||||
|
||||
error[E0107]: missing generics for struct `S`
|
||||
--> $DIR/recursive-delegation-ice-150152.rs:9:21
|
||||
|
|
||||
LL | impl Item for S<S> {
|
||||
| ^ expected 1 generic argument
|
||||
|
|
||||
note: struct defined here, with 1 generic parameter: `S`
|
||||
--> $DIR/recursive-delegation-ice-150152.rs:6:12
|
||||
|
|
||||
LL | struct S< S >;
|
||||
| ^ -
|
||||
help: add missing generic argument
|
||||
|
|
||||
LL | impl Item for S<S<S>> {
|
||||
| +++
|
||||
|
||||
error: aborting due to 6 previous errors
|
||||
|
||||
Some errors have detailed explanations: E0107, E0392, E0405, E0425, E0603.
|
||||
For more information about an error, try `rustc --explain E0107`.
|
||||
|
|
@ -1,9 +1,10 @@
|
|||
//@ check-pass
|
||||
//@ run-pass
|
||||
//@ edition:2018
|
||||
//@ aux-crate:recursive_delegation_aux=recursive-delegation-aux.rs
|
||||
|
||||
#![feature(fn_delegation)]
|
||||
#![allow(incomplete_features)]
|
||||
#![allow(warnings)]
|
||||
|
||||
mod first_mod {
|
||||
pub mod to_reuse {
|
||||
|
|
@ -12,18 +13,35 @@ mod first_mod {
|
|||
}
|
||||
}
|
||||
|
||||
mod single_reuse {
|
||||
reuse crate::first_mod::to_reuse::foo;
|
||||
reuse foo as bar;
|
||||
reuse foo as bar1;
|
||||
reuse bar as goo;
|
||||
reuse goo as koo;
|
||||
reuse koo as too;
|
||||
pub mod single_reuse {
|
||||
reuse crate::first_mod::to_reuse::foo { self + 1 }
|
||||
reuse foo as bar { self + 1 }
|
||||
reuse foo as bar1 { self + 1 }
|
||||
reuse bar as goo { self + 1 }
|
||||
reuse goo as koo { self + 1 }
|
||||
pub reuse koo as too { self + 1 }
|
||||
|
||||
pub fn check() {
|
||||
assert_eq!(foo(1), 3);
|
||||
assert_eq!(bar(1), 4);
|
||||
assert_eq!(bar1(1), 4);
|
||||
assert_eq!(goo(1), 5);
|
||||
assert_eq!(koo(1), 6);
|
||||
assert_eq!(too(1), 7);
|
||||
}
|
||||
}
|
||||
|
||||
mod glob_reuse {
|
||||
reuse super::to_reuse::{foo as bar, foo as bar1} { self }
|
||||
reuse super::glob_reuse::{bar as goo, goo as koo, koo as too} { self }
|
||||
|
||||
fn check() {
|
||||
bar(1);
|
||||
bar1(1);
|
||||
goo(1);
|
||||
koo(1);
|
||||
too(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -44,6 +62,29 @@ mod second_mod {
|
|||
|
||||
reuse TGlob::{foo1 as bar1, foo3 as bar3, bar1 as bar11, bar11 as bar111} { self.xd() }
|
||||
}
|
||||
|
||||
fn check() {
|
||||
struct X;
|
||||
impl T for X {
|
||||
fn foo(&self) {}
|
||||
fn bar(&self) {}
|
||||
fn goo(&self) {}
|
||||
fn poo(&self) {}
|
||||
}
|
||||
|
||||
impl TGlob for X {
|
||||
fn xd(&self) -> &Self { &self }
|
||||
fn foo1(&self) {}
|
||||
fn foo2(&self) {}
|
||||
fn foo3(&self) {}
|
||||
fn foo4(&self) {}
|
||||
|
||||
fn bar1(&self) {}
|
||||
fn bar3(&self) {}
|
||||
fn bar11(&self) {}
|
||||
fn bar111(&self) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mod third_mod {
|
||||
|
|
@ -63,6 +104,45 @@ mod third_mod {
|
|||
mod fourth_mod {
|
||||
reuse recursive_delegation_aux::goo as bar;
|
||||
reuse bar as foo;
|
||||
|
||||
fn check() {
|
||||
bar();
|
||||
foo();
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
mod fifth_mod {
|
||||
mod m {
|
||||
fn foo() { }
|
||||
pub reuse foo as bar;
|
||||
}
|
||||
|
||||
trait T {
|
||||
reuse m::bar as foo;
|
||||
}
|
||||
|
||||
struct X;
|
||||
impl T for X {}
|
||||
|
||||
trait T1 {
|
||||
reuse <X as T>::foo as baz;
|
||||
}
|
||||
|
||||
impl T1 for X {}
|
||||
|
||||
struct Y;
|
||||
impl T1 for Y {
|
||||
reuse <X as T>::foo as baz;
|
||||
}
|
||||
|
||||
fn check() {
|
||||
m::bar();
|
||||
<X as T>::foo();
|
||||
<X as T1>::baz();
|
||||
<Y as T1>::baz();
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
first_mod::single_reuse::check();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue