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
2 changes: 1 addition & 1 deletion compiler/rustc_ast_lowering/src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::{
AllowReturnTypeNotation, ImplTraitContext, ImplTraitPosition, ParamMode, ResolverAstLoweringExt,
};

impl<'a, 'hir> LoweringContext<'a, 'hir> {
impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
pub(crate) fn lower_inline_asm(
&mut self,
sp: Span,
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_ast_lowering/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use rustc_hir::Target;
use rustc_span::sym;
use smallvec::SmallVec;

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

impl<'a, 'hir> LoweringContext<'a, 'hir> {
impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
pub(super) fn lower_block(
&mut self,
b: &Block,
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_ast_lowering/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use std::sync::Arc;

use thin_vec::thin_vec;

use crate::LoweringContext;
use crate::{LoweringContext, ResolverAstLoweringExt};

impl<'a, 'hir> LoweringContext<'a, 'hir> {
impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
/// Lowered contracts are guarded with the `contract_checks` compiler flag,
/// i.e. the flag turns into a boolean guard in the lowered HIR. The reason
/// for not eliminating the contract code entirely when the `contract_checks`
Expand Down
39 changes: 20 additions & 19 deletions compiler/rustc_ast_lowering/src/delegation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
//! also be emitted during HIR ty lowering.

use std::iter;
use std::marker::PhantomData;

use ast::visit::Visitor;
use hir::def::{DefKind, PartialRes, Res};
Expand All @@ -51,7 +52,7 @@ use rustc_hir as hir;
use rustc_hir::attrs::{AttributeKind, InlineAttr};
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_middle::span_bug;
use rustc_middle::ty::{Asyncness, DelegationAttrs, DelegationFnSigAttrs, ResolverAstLowering};
use rustc_middle::ty::{Asyncness, DelegationAttrs, DelegationFnSigAttrs};
use rustc_span::symbol::kw;
use rustc_span::{DUMMY_SP, Ident, Span, Symbol};
use smallvec::SmallVec;
Expand Down Expand Up @@ -134,16 +135,14 @@ impl DelegationIds {
}
}

impl<'hir> LoweringContext<'_, 'hir> {
impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
fn is_method(&self, def_id: DefId, span: Span) -> bool {
match self.tcx.def_kind(def_id) {
DefKind::Fn => false,
DefKind::AssocFn => match def_id.as_local() {
Some(local_def_id) => self
.resolver
.delegation_fn_sigs
.get(&local_def_id)
.is_some_and(|sig| sig.has_self),
Some(local_def_id) => {
self.resolver.delegation_fn_sig(local_def_id).is_some_and(|sig| sig.has_self)
}
None => self.tcx.associated_item(def_id).is_method(),
},
_ => span_bug!(span, "unexpected DefKind for delegation item"),
Expand All @@ -159,7 +158,7 @@ impl<'hir> LoweringContext<'_, 'hir> {

// Delegation can be unresolved in illegal places such as function bodies in extern blocks (see #151356)
let ids = if let Some(delegation_info) =
self.resolver.delegation_infos.get(&self.local_def_id(item_id))
self.resolver.delegation_info(self.local_def_id(item_id))
{
self.get_delegation_ids(delegation_info.resolution_node, span)
} else {
Expand Down Expand Up @@ -344,10 +343,10 @@ impl<'hir> LoweringContext<'_, 'hir> {

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) {
if let Some(fn_sig) = self.resolver.delegation_fn_sig(local_id) {
&fn_sig.attrs
} else {
&self.resolver.delegation_infos[&local_id].attrs
&self.resolver.delegation_info(local_id).expect("processing delegation").attrs
}
}

Expand Down Expand Up @@ -378,7 +377,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
// 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(delegation_info) = self.resolver.delegation_infos.get(&local_id)
&& let Some(delegation_info) = self.resolver.delegation_info(local_id)
{
node_id = delegation_info.resolution_node;
if visited.contains(&node_id) {
Expand All @@ -402,7 +401,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
// Function parameter count, including C variadic `...` if present.
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) {
match self.resolver.delegation_fn_sig(local_sig_id) {
Some(sig) => (sig.param_count, sig.c_variadic),
None => (0, false),
}
Expand Down Expand Up @@ -457,7 +456,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
span: Span,
) -> hir::FnSig<'hir> {
let header = if let Some(local_sig_id) = sig_id.as_local() {
match self.resolver.delegation_fn_sigs.get(&local_sig_id) {
match self.resolver.delegation_fn_sig(local_sig_id) {
Some(sig) => {
let parent = self.tcx.parent(sig_id);
// HACK: we override the default safety instead of generating attributes from the ether.
Expand Down Expand Up @@ -573,6 +572,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
resolver: this.resolver,
path_id: delegation.id,
self_param_id: pat_node_id,
phantom: PhantomData,
};
self_resolver.visit_block(block);
// Target expr needs to lower `self` path.
Expand Down Expand Up @@ -816,25 +816,26 @@ impl<'hir> LoweringContext<'_, 'hir> {
}
}

struct SelfResolver<'a, 'tcx> {
resolver: &'a mut ResolverAstLowering<'tcx>,
struct SelfResolver<'a, 'tcx, R> {
resolver: &'a mut R,
path_id: NodeId,
self_param_id: NodeId,
phantom: PhantomData<&'tcx ()>,
}

impl SelfResolver<'_, '_> {
impl<'tcx, R: ResolverAstLoweringExt<'tcx>> SelfResolver<'_, 'tcx, R> {
fn try_replace_id(&mut self, id: NodeId) {
if let Some(res) = self.resolver.partial_res_map.get(&id)
if let Some(res) = self.resolver.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.resolver.insert_partial_res(id, new_res);
}
}
}

impl<'ast, 'a> Visitor<'ast> for SelfResolver<'a, '_> {
impl<'ast, 'a, 'tcx, R: ResolverAstLoweringExt<'tcx>> Visitor<'ast> for SelfResolver<'a, 'tcx, R> {
fn visit_id(&mut self, id: NodeId) {
self.try_replace_id(id);
}
Expand Down
18 changes: 9 additions & 9 deletions compiler/rustc_ast_lowering/src/delegation/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rustc_span::symbol::kw;
use rustc_span::{DUMMY_SP, Ident, Span};
use thin_vec::{ThinVec, thin_vec};

use crate::{AstOwner, LoweringContext};
use crate::{AstOwner, LoweringContext, ResolverAstLoweringExt};

pub(super) enum DelegationGenerics<T> {
/// User-specified args are present: `reuse foo::<String>;`.
Expand Down Expand Up @@ -60,7 +60,7 @@ impl<T> DelegationGenerics<T> {
impl<'hir> HirOrAstGenerics<'hir> {
pub(super) fn into_hir_generics(
&mut self,
ctx: &mut LoweringContext<'_, 'hir>,
ctx: &mut LoweringContext<'_, 'hir, impl ResolverAstLoweringExt<'hir>>,
item_id: NodeId,
span: Span,
) -> &mut HirOrAstGenerics<'hir> {
Expand Down Expand Up @@ -100,7 +100,7 @@ impl<'hir> HirOrAstGenerics<'hir> {

pub(super) fn into_generic_args(
&self,
ctx: &mut LoweringContext<'_, 'hir>,
ctx: &mut LoweringContext<'_, 'hir, impl ResolverAstLoweringExt<'hir>>,
add_lifetimes: bool,
span: Span,
) -> Option<&'hir hir::GenericArgs<'hir>> {
Expand Down Expand Up @@ -140,7 +140,7 @@ impl<'hir> GenericsGenerationResults<'hir> {
&mut self,
item_id: NodeId,
span: Span,
ctx: &mut LoweringContext<'_, 'hir>,
ctx: &mut LoweringContext<'_, 'hir, impl ResolverAstLoweringExt<'hir>>,
) -> impl Iterator<Item = hir::GenericParam<'hir>> {
// Now we always call `into_hir_generics` both on child and parent,
// however in future we would not do that, when scenarios like
Expand Down Expand Up @@ -182,7 +182,7 @@ impl<'hir> GenericsGenerationResults<'hir> {
&mut self,
item_id: NodeId,
span: Span,
ctx: &mut LoweringContext<'_, 'hir>,
ctx: &mut LoweringContext<'_, 'hir, impl ResolverAstLoweringExt<'hir>>,
) -> impl Iterator<Item = hir::WherePredicate<'hir>> {
// Now we always call `into_hir_generics` both on child and parent,
// however in future we would not do that, when scenarios like
Expand All @@ -207,7 +207,7 @@ impl<'hir> GenericsGenerationResults<'hir> {
}
}

impl<'hir> LoweringContext<'_, 'hir> {
impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
pub(super) fn lower_delegation_generics(
&mut self,
delegation: &Delegation,
Expand Down Expand Up @@ -289,11 +289,11 @@ impl<'hir> LoweringContext<'_, 'hir> {

// Note that we use self.disambiguator here, if we will create new every time
// we will get ICE if params have the same name.
self.resolver.node_id_to_def_id.insert(
self.resolver.insert_new_def_id(
p.id,
self.tcx
.create_def(
self.resolver.node_id_to_def_id[&item_id],
self.local_def_id(item_id),
Some(p.ident.name),
match p.kind {
GenericParamKind::Lifetime => DefKind::LifetimeParam,
Expand Down Expand Up @@ -509,7 +509,7 @@ impl<'hir> LoweringContext<'_, 'hir> {

let node_id = self.next_node_id();

self.resolver.partial_res_map.insert(node_id, hir::def::PartialRes::new(res));
self.resolver.insert_partial_res(node_id, hir::def::PartialRes::new(res));

GenericParamKind::Const {
ty: Box::new(Ty {
Expand Down
7 changes: 5 additions & 2 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl<'v> rustc_ast::visit::Visitor<'v> for WillCreateDefIdsVisitor {
}
}

impl<'hir> LoweringContext<'_, 'hir> {
impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
fn lower_exprs(&mut self, exprs: &[Box<Expr>]) -> &'hir [hir::Expr<'hir>] {
self.arena.alloc_from_iter(exprs.iter().map(|x| self.lower_expr_mut(x)))
}
Expand Down Expand Up @@ -1232,7 +1232,10 @@ 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<'hir>(
lower_ctx: &mut LoweringContext<'_, 'hir, impl ResolverAstLoweringExt<'hir>>,
lhs: &Expr,
) -> bool {
match &lhs.kind {
ExprKind::Array(..)
| ExprKind::Struct(..)
Expand Down
7 changes: 4 additions & 3 deletions compiler/rustc_ast_lowering/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ use rustc_session::config::FmtDebug;
use rustc_span::{ByteSymbol, DesugaringKind, Ident, Span, Symbol, sym};

use super::LoweringContext;
use crate::ResolverAstLoweringExt;

impl<'hir> LoweringContext<'_, 'hir> {
impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
pub(crate) fn lower_format_args(&mut self, sp: Span, fmt: &FormatArgs) -> hir::ExprKind<'hir> {
// Never call the const constructor of `fmt::Arguments` if the
// format_args!() had any arguments _before_ flattening/inlining.
Expand Down Expand Up @@ -230,7 +231,7 @@ enum ArgumentType {
/// <core::fmt::Argument>::new_…(arg)
/// ```
fn make_argument<'hir>(
ctx: &mut LoweringContext<'_, 'hir>,
ctx: &mut LoweringContext<'_, 'hir, impl ResolverAstLoweringExt<'hir>>,
sp: Span,
arg: &'hir hir::Expr<'hir>,
ty: ArgumentType,
Expand Down Expand Up @@ -277,7 +278,7 @@ fn make_count(
}

fn expand_format_args<'hir>(
ctx: &mut LoweringContext<'_, 'hir>,
ctx: &mut LoweringContext<'_, 'hir, impl ResolverAstLoweringExt<'hir>>,
macsp: Span,
fmt: &FormatArgs,
allow_const: bool,
Expand Down
18 changes: 9 additions & 9 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rustc_hir::{
};
use rustc_index::{IndexSlice, IndexVec};
use rustc_middle::span_bug;
use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
use rustc_middle::ty::TyCtxt;
use rustc_span::def_id::DefId;
use rustc_span::edit_distance::find_best_match_for_name;
use rustc_span::{DUMMY_SP, DesugaringKind, Ident, Span, Symbol, kw, sym};
Expand All @@ -25,9 +25,9 @@ use super::{
RelaxedBoundForbiddenReason, RelaxedBoundPolicy, ResolverAstLoweringExt,
};

pub(super) struct ItemLowerer<'a, 'hir> {
pub(super) struct ItemLowerer<'a, 'hir, R> {
pub(super) tcx: TyCtxt<'hir>,
pub(super) resolver: &'a mut ResolverAstLowering<'hir>,
pub(super) resolver: &'a mut R,
pub(super) ast_index: &'a IndexSlice<LocalDefId, AstOwner<'a>>,
pub(super) owners: &'a mut IndexVec<LocalDefId, hir::MaybeOwner<'hir>>,
}
Expand All @@ -51,11 +51,11 @@ fn add_ty_alias_where_clause(
if before.0 || !after.0 { before } else { after };
}

impl<'a, 'hir> ItemLowerer<'a, 'hir> {
impl<'hir, R: ResolverAstLoweringExt<'hir>> ItemLowerer<'_, 'hir, R> {
fn with_lctx(
&mut self,
owner: NodeId,
f: impl FnOnce(&mut LoweringContext<'_, 'hir>) -> hir::OwnerNode<'hir>,
f: impl FnOnce(&mut LoweringContext<'_, 'hir, R>) -> hir::OwnerNode<'hir>,
) {
let mut lctx = LoweringContext::new(self.tcx, self.ast_index, self.resolver);
lctx.with_hir_id_owner(owner, |lctx| f(lctx));
Expand All @@ -77,7 +77,7 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
match node {
AstOwner::NonOwner => {}
AstOwner::Crate(c) => {
assert_eq!(self.resolver.node_id_to_def_id[&CRATE_NODE_ID], CRATE_DEF_ID);
assert_eq!(self.resolver.local_def_id(CRATE_NODE_ID), CRATE_DEF_ID);
self.with_lctx(CRATE_NODE_ID, |lctx| {
let module = lctx.lower_mod(&c.items, &c.spans);
// FIXME(jdonszelman): is dummy span ever a problem here?
Expand All @@ -99,7 +99,7 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
}
}

impl<'hir> LoweringContext<'_, 'hir> {
impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
pub(super) fn lower_mod(
&mut self,
items: &[Box<Item>],
Expand Down Expand Up @@ -1475,7 +1475,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
pub(crate) fn lower_coroutine_body_with_moved_arguments(
&mut self,
decl: &FnDecl,
lower_body: impl FnOnce(&mut LoweringContext<'_, 'hir>) -> hir::Expr<'hir>,
lower_body: impl FnOnce(&mut LoweringContext<'_, 'hir, R>) -> hir::Expr<'hir>,
fn_decl_span: Span,
body_span: Span,
coroutine_kind: CoroutineKind,
Expand Down Expand Up @@ -1612,7 +1612,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
parameters.push(new_parameter);
}

let mkbody = |this: &mut LoweringContext<'_, 'hir>| {
let mkbody = |this: &mut LoweringContext<'_, 'hir, R>| {
// Create a block from the user's function body:
let user_body = lower_body(this);

Expand Down
Loading
Loading