Skip to content
Merged
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
1 change: 0 additions & 1 deletion Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3640,7 +3640,6 @@ version = "0.0.0"
dependencies = [
"rustc_abi",
"rustc_ast",
"rustc_ast_pretty",
"rustc_attr_parsing",
"rustc_data_structures",
"rustc_errors",
Expand Down
18 changes: 18 additions & 0 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_data_structures::tagged_ptr::Tag;
use rustc_macros::{Decodable, Encodable, StableHash, Walkable};
pub use rustc_span::AttrId;
use rustc_span::def_id::LocalDefId;
use rustc_span::{
ByteSymbol, DUMMY_SP, ErrorGuaranteed, Ident, LocalExpnId, Span, Spanned, Symbol, kw, respan,
sym,
Expand Down Expand Up @@ -4362,6 +4363,23 @@ impl TryFrom<ItemKind> for ForeignItemKind {

pub type ForeignItem = Item<ForeignItemKind>;

/// Fragment of the AST according to "HIR owner" semantics.
///
/// This is used to map each `LocalDefId` to its content's AST.
#[derive(Debug)]
pub enum AstOwner {
Comment thread
petrochenkov marked this conversation as resolved.
/// This definition does not correspond to a HIR owner.
NonOwner,
/// This definition corresponds to a nested `use` tree.
/// The `LocalDefId` points to its HIR owner.
NestedUseTree(LocalDefId),
Crate(Box<Crate>),
Item(Box<Item>),
TraitItem(Box<AssocItem>),
ImplItem(Box<AssocItem>),
ForeignItem(Box<ForeignItem>),
}

// Some nodes are used a lot. Make sure they don't unintentionally get bigger.
#[cfg(target_pointer_width = "64")]
mod size_asserts {
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_ast/src/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,10 @@ pub fn walk_flat_map_stmt<T: MutVisitor>(
stmts
}

fn walk_flat_map_stmt_kind<T: MutVisitor>(vis: &mut T, kind: StmtKind) -> SmallVec<[StmtKind; 1]> {
pub fn walk_flat_map_stmt_kind<T: MutVisitor>(
vis: &mut T,
kind: StmtKind,
) -> SmallVec<[StmtKind; 1]> {
match kind {
StmtKind::Let(mut local) => smallvec![StmtKind::Let({
vis.visit_local(&mut local);
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_ast_lowering/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ doctest = false
# tidy-alphabetical-start
rustc_abi = { path = "../rustc_abi" }
rustc_ast = { path = "../rustc_ast" }
rustc_ast_pretty = { path = "../rustc_ast_pretty" }
rustc_attr_parsing = { path = "../rustc_attr_parsing" }
rustc_data_structures = { path = "../rustc_data_structures" }
rustc_errors = { path = "../rustc_errors" }
Expand Down
16 changes: 5 additions & 11 deletions compiler/rustc_ast_lowering/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
stmts.push(hir::Stmt { hir_id, kind, span });
}
StmtKind::Item(it) => {
stmts.extend(self.lower_item_ref(it).into_iter().enumerate().map(
|(i, item_id)| {
let hir_id = match i {
0 => self.lower_node_id(s.id),
_ => self.next_id(),
};
let kind = hir::StmtKind::Item(item_id);
let span = self.lower_span(s.span);
hir::Stmt { hir_id, kind, span }
},
));
let item_id = self.lower_item_ref(it);
let hir_id = self.lower_node_id(s.id);
let kind = hir::StmtKind::Item(item_id);
let span = self.lower_span(s.span);
stmts.push(hir::Stmt { hir_id, kind, span });
}
StmtKind::Expr(e) => {
let e = self.lower_expr(e);
Expand Down
104 changes: 40 additions & 64 deletions compiler/rustc_ast_lowering/src/delegation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ use rustc_abi::ExternAbi;
use rustc_ast as ast;
use rustc_ast::node_id::NodeMap;
use rustc_ast::*;
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
use rustc_data_structures::fx::FxHashSet;
use rustc_hir::attrs::{AttributeKind, InlineAttr};
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::{self as hir, FnDeclFlags};
use rustc_middle::span_bug;
use rustc_middle::ty::{Asyncness, PerOwnerResolverData, TyCtxt};
use rustc_middle::ty::{Asyncness, PerOwnerResolverData};
use rustc_span::def_id::{DefId, LocalDefId};
use rustc_span::symbol::kw;
use rustc_span::{ErrorGuaranteed, Ident, Span, Symbol};

Expand All @@ -62,7 +62,6 @@ use crate::diagnostics::{
};
use crate::{
AllowReturnTypeNotation, ImplTraitContext, ImplTraitPosition, LoweringContext, ParamMode,
index_crate,
};

mod generics;
Expand Down Expand Up @@ -121,64 +120,6 @@ static ATTRS_ADDITIONS: &[AttrAdditionInfo] = &[
},
];

pub(crate) fn delegations_resolutions(
tcx: TyCtxt<'_>,
_: (),
) -> FxIndexMap<LocalDefId, Result<DefId, ErrorGuaranteed>> {
let krate = tcx.hir_crate(());

let (resolver, ast_crate) = &*krate.delayed_resolver.borrow();

// FIXME!!!(fn_delegation): make ast index lifetime same as resolver,
// as it is too bad to reindex whole crate on each delegation lowering.
let ast_index = index_crate(resolver, ast_crate);

let mut result = FxIndexMap::<LocalDefId, Result<DefId, ErrorGuaranteed>>::default();

for &def_id in &krate.delayed_ids {
let delegation = ast_index[def_id].delegation().expect("processing delegations");
let span = delegation.last_segment_span();

if let Some(info) = tcx.resolutions(()).delegation_infos.get(&def_id) {
let res = info.resolution_id.map(|id| check_for_cycles(tcx, id, span).map(|_| id));
result.insert(def_id, res.flatten());
} else {
tcx.dcx().span_delayed_bug(
span,
format!("delegation resolution record was not found for {def_id:?}"),
);
}
}

result
}

fn check_for_cycles(tcx: TyCtxt<'_>, mut def_id: DefId, span: Span) -> Result<(), ErrorGuaranteed> {
let mut visited: FxHashSet<DefId> = Default::default();

loop {
visited.insert(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(info) = tcx.resolutions(()).delegation_infos.get(&local_id)
&& let Ok(id) = info.resolution_id
{
def_id = id;
if visited.contains(&def_id) {
return Err(match visited.len() {
1 => tcx.dcx().emit_err(UnresolvedDelegationCallee { span }),
_ => tcx.dcx().emit_err(CycleInDelegationSignatureResolution { span }),
});
}
} else {
return Ok(());
}
}
}

impl<'hir> LoweringContext<'_, 'hir> {
fn is_method(&self, def_id: DefId, span: Span) -> bool {
match self.tcx.def_kind(def_id) {
Expand All @@ -188,18 +129,53 @@ impl<'hir> LoweringContext<'_, 'hir> {
}
}

fn check_for_cycles(&self, mut def_id: DefId, span: Span) -> Result<(), ErrorGuaranteed> {
let mut visited: FxHashSet<DefId> = Default::default();

loop {
visited.insert(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(info) = self.tcx.resolutions(()).delegation_infos.get(&local_id)
&& let Ok(id) = info.resolution_id
{
def_id = id;
if visited.contains(&def_id) {
return Err(match visited.len() {
1 => self.dcx().emit_err(UnresolvedDelegationCallee { span }),
_ => self.dcx().emit_err(CycleInDelegationSignatureResolution { span }),
});
}
} else {
return Ok(());
}
}
}

pub(crate) fn lower_delegation(
&mut self,
delegation: &Delegation,
item_id: NodeId,
) -> DelegationResults<'hir> {
let span = self.lower_span(delegation.last_segment_span());

let sig_id = self.tcx.delegations_resolutions(()).get(&self.owner.def_id).copied();
let Some(info) = self.tcx.resolutions(()).delegation_infos.get(&self.owner.def_id) else {
self.dcx().span_delayed_bug(
span,
format!("delegation resolution record was not found for {:?}", self.owner.def_id),
);

return self.generate_delegation_error(span, delegation);
};

let sig_id = info.resolution_id.and_then(|id| self.check_for_cycles(id, span).map(|_| id));

// Delegation can be missing from the `delegations_resolutions` table
// in illegal places such as function bodies in extern blocks (see #151356).
let Some(Ok(sig_id)) = sig_id else {
let Ok(sig_id) = sig_id else {
self.dcx().span_delayed_bug(
span,
format!("LoweringContext: the delegation {:?} is unresolved", item_id),
Expand Down
12 changes: 7 additions & 5 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::sync::Arc;

use rustc_ast::node_id::NodeMap;
use rustc_ast::*;
use rustc_ast_pretty::pprust::expr_to_string;
use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_errors::msg;
use rustc_hir as hir;
Expand Down Expand Up @@ -557,13 +556,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
let mut invalid_expr_error = |tcx: TyCtxt<'_>, span| {
// Avoid emitting the error multiple times.
if error.is_none() {
let sm = tcx.sess.source_map();
let mut const_args = vec![];
let mut other_args = vec![];
for (idx, arg) in args.iter().enumerate() {
if legacy_args_idx.contains(&idx) {
const_args.push(format!("{{ {} }}", expr_to_string(arg)));
} else {
other_args.push(expr_to_string(arg));
if let Ok(arg) = sm.span_to_snippet(arg.span) {
if legacy_args_idx.contains(&idx) {
const_args.push(format!("{{ {} }}", arg));
} else {
other_args.push(arg);
}
}
}
let suggestion = UseConstGenericArg {
Expand Down
Loading
Loading