Skip to content

Make lowering incremental, take 3/N #142830

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from
Draft
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 @@ -3354,7 +3354,6 @@ version = "0.0.0"
dependencies = [
"rustc_abi",
"rustc_ast",
"rustc_ast_pretty",
"rustc_attr_data_structures",
"rustc_attr_parsing",
"rustc_data_structures",
Expand Down
11 changes: 11 additions & 0 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4054,6 +4054,17 @@ impl TryFrom<ItemKind> for ForeignItemKind {

pub type ForeignItem = Item<ForeignItemKind>;

#[derive(Debug)]
pub enum AstOwner {
NonOwner,
Synthetic(rustc_span::def_id::LocalDefId),
Crate(P<Crate>),
Item(P<Item>),
TraitItem(P<AssocItem>),
ImplItem(P<AssocItem>),
ForeignItem(P<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 @@ -142,7 +142,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_data_structures = { path = "../rustc_attr_data_structures" }
rustc_attr_parsing = { path = "../rustc_attr_parsing" }
rustc_data_structures = { path = "../rustc_data_structures" }
Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_ast_lowering/src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ use super::errors::{
};
use crate::{
AllowReturnTypeNotation, ImplTraitContext, ImplTraitPosition, ParamMode,
ResolverAstLoweringExt, fluent_generated as fluent,
fluent_generated as fluent,
};

impl<'a, 'hir> LoweringContext<'a, 'hir> {
impl<'hir> LoweringContext<'hir> {
pub(crate) fn lower_inline_asm(
&mut self,
sp: Span,
Expand Down Expand Up @@ -200,7 +200,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
},
InlineAsmOperand::Sym { sym } => {
let static_def_id = self
.resolver
.get_partial_res(sym.id)
.and_then(|res| res.full_res())
.and_then(|res| match res {
Expand Down
18 changes: 6 additions & 12 deletions compiler/rustc_ast_lowering/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use smallvec::SmallVec;

use crate::{ImplTraitContext, ImplTraitPosition, LoweringContext};

impl<'a, 'hir> LoweringContext<'a, 'hir> {
impl<'hir> LoweringContext<'hir> {
pub(super) fn lower_block(
&mut self,
b: &Block,
Expand Down Expand Up @@ -43,17 +43,11 @@ impl<'a, 'hir> LoweringContext<'a, '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
82 changes: 47 additions & 35 deletions compiler/rustc_ast_lowering/src/delegation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,15 @@ use rustc_ast::*;
use rustc_errors::ErrorGuaranteed;
use rustc_hir::def_id::DefId;
use rustc_middle::span_bug;
use rustc_middle::ty::{Asyncness, ResolverAstLowering};
use rustc_middle::ty::Asyncness;
use rustc_span::symbol::kw;
use rustc_span::{Ident, Span, Symbol};
use {rustc_ast as ast, rustc_hir as hir};

use super::{GenericArgsMode, ImplTraitContext, LoweringContext, ParamMode};
use crate::{AllowReturnTypeNotation, ImplTraitPosition, ResolverAstLoweringExt};
use super::{
AllowReturnTypeNotation, GenericArgsMode, ImplTraitContext, ImplTraitPosition, LoweringContext,
ParamMode,
};

pub(crate) struct DelegationResults<'hir> {
pub body_id: hir::BodyId,
Expand All @@ -60,22 +63,7 @@ pub(crate) struct DelegationResults<'hir> {
pub generics: &'hir hir::Generics<'hir>,
}

impl<'hir> LoweringContext<'_, 'hir> {
/// Defines whether the delegatee is an associated function whose first parameter is `self`.
pub(crate) fn delegatee_is_method(
&self,
item_id: NodeId,
path_id: NodeId,
span: Span,
is_in_trait_impl: bool,
) -> bool {
let sig_id = self.get_delegation_sig_id(item_id, path_id, span, is_in_trait_impl);
let Ok(sig_id) = sig_id else {
return false;
};
self.is_method(sig_id, span)
}

impl<'hir> LoweringContext<'hir> {
fn is_method(&self, def_id: DefId, span: Span) -> bool {
match self.tcx.def_kind(def_id) {
DefKind::Fn => false,
Expand All @@ -101,10 +89,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
let sig_id = self.get_delegation_sig_id(item_id, delegation.id, span, is_in_trait_impl);
match sig_id {
Ok(sig_id) => {
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, param_count, 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);
DelegationResults { body_id, sig, ident, generics }
Expand All @@ -125,8 +114,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
}

fn get_resolution_id(&self, node_id: NodeId, span: Span) -> Result<DefId, ErrorGuaranteed> {
let def_id =
self.resolver.get_partial_res(node_id).and_then(|r| r.expect_full_res().opt_def_id());
let def_id = self.get_partial_res(node_id).and_then(|r| r.expect_full_res().opt_def_id());
def_id.ok_or_else(|| {
self.tcx.dcx().span_delayed_bug(
span,
Expand Down Expand Up @@ -234,10 +222,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
hir::FnSig { decl, header, span }
}

fn generate_param(&mut self, idx: usize, span: Span) -> (hir::Param<'hir>, NodeId) {
fn generate_param(
&mut self,
is_method: bool,
idx: usize,
span: Span,
) -> (hir::Param<'hir>, NodeId) {
let pat_node_id = self.next_node_id();
let pat_id = self.lower_node_id(pat_node_id);
let ident = Ident::with_dummy_span(Symbol::intern(&format!("arg{idx}")));
// FIXME(cjgillot) AssocItem currently relies on self parameter being exactly named `self`.
let name = if is_method && idx == 0 {
kw::SelfLower
} else {
Symbol::intern(&format!("arg{idx}"))
};
let ident = Ident::with_dummy_span(name);
let pat = self.arena.alloc(hir::Pat {
hir_id: pat_id,
kind: hir::PatKind::Binding(hir::BindingMode::NONE, pat_id, ident, None),
Expand All @@ -248,9 +247,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
(hir::Param { hir_id: self.next_id(), pat, ty_span: span, span }, pat_node_id)
}

fn generate_arg(&mut self, idx: usize, param_id: HirId, span: Span) -> hir::Expr<'hir> {
fn generate_arg(
&mut self,
is_method: bool,
idx: usize,
param_id: HirId,
span: Span,
) -> hir::Expr<'hir> {
// FIXME(cjgillot) AssocItem currently relies on self parameter being exactly named `self`.
let name = if is_method && idx == 0 {
kw::SelfLower
} else {
Symbol::intern(&format!("arg{idx}"))
};
let segments = self.arena.alloc_from_iter(iter::once(hir::PathSegment {
ident: Ident::with_dummy_span(Symbol::intern(&format!("arg{idx}"))),
ident: Ident::with_dummy_span(name),
hir_id: self.next_id(),
res: Res::Local(param_id),
args: None,
Expand All @@ -264,6 +275,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
fn lower_delegation_body(
&mut self,
delegation: &Delegation,
is_method: bool,
param_count: usize,
span: Span,
) -> BodyId {
Expand All @@ -274,14 +286,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
let mut args: Vec<hir::Expr<'_>> = Vec::with_capacity(param_count);

for idx in 0..param_count {
let (param, pat_node_id) = this.generate_param(idx, span);
let (param, pat_node_id) = this.generate_param(is_method, idx, span);
parameters.push(param);

let arg = if let Some(block) = block
&& idx == 0
{
let mut self_resolver = SelfResolver {
resolver: this.resolver,
ctxt: this,
path_id: delegation.id,
self_param_id: pat_node_id,
};
Expand All @@ -290,7 +302,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
this.ident_and_label_to_local_id.insert(pat_node_id, param.pat.hir_id.local_id);
this.lower_target_expr(&block)
} else {
this.generate_arg(idx, param.pat.hir_id, span)
this.generate_arg(is_method, idx, param.pat.hir_id, span)
};
args.push(arg);
}
Expand Down Expand Up @@ -427,25 +439,25 @@ impl<'hir> LoweringContext<'_, 'hir> {
}
}

struct SelfResolver<'a> {
resolver: &'a mut ResolverAstLowering,
struct SelfResolver<'r, 'hir> {
ctxt: &'r mut LoweringContext<'hir>,
path_id: NodeId,
self_param_id: NodeId,
}

impl<'a> SelfResolver<'a> {
impl SelfResolver<'_, '_> {
fn try_replace_id(&mut self, id: NodeId) {
if let Some(res) = self.resolver.partial_res_map.get(&id)
if let Some(res) = self.ctxt.get_partial_res(id)
&& let Some(Res::Local(sig_id)) = res.full_res()
&& sig_id == self.path_id
{
let new_res = PartialRes::new(Res::Local(self.self_param_id));
self.resolver.partial_res_map.insert(id, new_res);
self.ctxt.partial_res_overrides.insert(id, new_res);
}
}
}

impl<'ast, 'a> Visitor<'ast> for SelfResolver<'a> {
impl<'ast> Visitor<'ast> for SelfResolver<'_, '_> {
fn visit_id(&mut self, id: NodeId) {
self.try_replace_id(id);
}
Expand Down
29 changes: 19 additions & 10 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ use std::sync::Arc;

use rustc_ast::ptr::P as AstP;
use rustc_ast::*;
use rustc_ast_pretty::pprust::expr_to_string;
use rustc_attr_data_structures::{AttributeKind, find_attr};
use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_hir as hir;
use rustc_hir::HirId;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::definitions::DefPathData;
use rustc_middle::span_bug;
use rustc_middle::ty::TyCtxt;
use rustc_session::errors::report_lit_error;
Expand Down Expand Up @@ -52,7 +52,7 @@ impl<'v> rustc_ast::visit::Visitor<'v> for WillCreateDefIdsVisitor {
}
}

impl<'hir> LoweringContext<'_, 'hir> {
impl<'hir> LoweringContext<'hir> {
fn lower_exprs(&mut self, exprs: &[AstP<Expr>]) -> &'hir [hir::Expr<'hir>] {
self.arena.alloc_from_iter(exprs.iter().map(|x| self.lower_expr_mut(x)))
}
Expand Down Expand Up @@ -462,13 +462,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 All @@ -488,7 +491,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
for (idx, arg) in args.iter().cloned().enumerate() {
if legacy_args_idx.contains(&idx) {
let node_id = self.next_node_id();
self.create_def(node_id, None, DefKind::AnonConst, f.span);
self.create_def(
node_id,
None,
DefKind::AnonConst,
DefPathData::LateAnonConst,
f.span,
);
let mut visitor = WillCreateDefIdsVisitor {};
let const_value = if let ControlFlow::Break(span) = visitor.visit_expr(&arg) {
AstP(Expr {
Expand Down Expand Up @@ -1260,7 +1269,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
whole_span: Span,
) -> hir::ExprKind<'hir> {
// Return early in case of an ordinary assignment.
fn is_ordinary(lower_ctx: &mut LoweringContext<'_, '_>, lhs: &Expr) -> bool {
fn is_ordinary(lower_ctx: &mut LoweringContext<'_>, lhs: &Expr) -> bool {
match &lhs.kind {
ExprKind::Array(..)
| ExprKind::Struct(..)
Expand Down Expand Up @@ -1320,7 +1329,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
) -> Option<(&'a Option<AstP<QSelf>>, &'a Path)> {
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 let Some(partial_res) = self.get_partial_res(expr.id) {
if let Some(res) = partial_res.full_res()
&& !res.expected_in_tuple_struct_pat()
{
Expand All @@ -1342,7 +1351,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
) -> Option<(&'a Option<AstP<QSelf>>, &'a Path)> {
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 let Some(partial_res) = self.get_partial_res(expr.id) {
if let Some(res) = partial_res.full_res()
&& !res.expected_in_unit_struct_pat()
{
Expand Down
Loading
Loading