mirror of
https://github.com/rust-lang/rust.git
synced 2026-04-27 18:57:42 +03:00
Auto merge of #153656 - aerooneqq:split-ast-lowering-resolver, r=petrochenkov
Abstract AST lowering resolver This PR adds new methods for `ResolverAstLoweringExt` for future use in rust-lang/rust#153489 and abstracts resolver in lowering through generic parameters. > ~This PR splits resolver for AST lowering into two parts: mutable and readonly. This will allow us to use borrowed resolver in rust-lang/rust#153489, when we will not steal but borrow resolver from resolve stage.~ Second step for rust-lang/rust#153489. r? @petrochenkov
This commit is contained in:
@@ -23,7 +23,7 @@
|
||||
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,
|
||||
|
||||
@@ -4,9 +4,9 @@
|
||||
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,
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
|
||||
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`
|
||||
|
||||
@@ -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};
|
||||
@@ -51,7 +52,7 @@
|
||||
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;
|
||||
@@ -134,16 +135,14 @@ fn delegee_id(&self) -> DefId {
|
||||
}
|
||||
}
|
||||
|
||||
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"),
|
||||
@@ -159,7 +158,7 @@ pub(crate) fn lower_delegation(
|
||||
|
||||
// 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 {
|
||||
@@ -344,10 +343,10 @@ fn parse_local_original_attrs(&self, def_id: DefId) -> Option<Vec<hir::Attribute
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -378,7 +377,7 @@ fn get_delegation_ids(
|
||||
// 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) {
|
||||
@@ -402,7 +401,7 @@ fn get_resolution_id(&self, node_id: NodeId) -> Option<DefId> {
|
||||
// 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),
|
||||
}
|
||||
@@ -457,7 +456,7 @@ fn lower_delegation_sig(
|
||||
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.
|
||||
@@ -573,6 +572,7 @@ fn lower_delegation_body(
|
||||
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.
|
||||
@@ -818,25 +818,26 @@ fn mk_expr(&mut self, kind: hir::ExprKind<'hir>, span: Span) -> hir::Expr<'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);
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
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>;`.
|
||||
@@ -81,7 +81,7 @@ fn args_propagation_details(&self) -> GenericArgsPropagationDetails {
|
||||
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> {
|
||||
@@ -128,7 +128,7 @@ fn hir_generics_or_empty(&self) -> &'hir hir::Generics<'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>> {
|
||||
@@ -169,7 +169,7 @@ pub(super) fn all_params(
|
||||
&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
|
||||
@@ -211,7 +211,7 @@ pub(super) fn all_predicates(
|
||||
&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
|
||||
@@ -236,7 +236,7 @@ pub(super) fn all_predicates(
|
||||
}
|
||||
}
|
||||
|
||||
impl<'hir> LoweringContext<'_, 'hir> {
|
||||
impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
|
||||
pub(super) fn lower_delegation_generics(
|
||||
&mut self,
|
||||
delegation: &Delegation,
|
||||
@@ -334,11 +334,11 @@ fn lower_delegation_generic_params(
|
||||
|
||||
// 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,
|
||||
@@ -554,7 +554,7 @@ fn map_const_kind(&mut self, p: &ty::GenericParamDef, span: Span) -> GenericPara
|
||||
|
||||
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 {
|
||||
|
||||
@@ -52,7 +52,7 @@ fn visit_expr(&mut self, ex: &'v Expr) -> Self::Result {
|
||||
}
|
||||
}
|
||||
|
||||
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)))
|
||||
}
|
||||
@@ -1244,7 +1244,10 @@ fn lower_expr_assign(
|
||||
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(..)
|
||||
|
||||
@@ -7,8 +7,9 @@
|
||||
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.
|
||||
@@ -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,
|
||||
@@ -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,
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
};
|
||||
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};
|
||||
@@ -27,9 +27,9 @@
|
||||
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>>,
|
||||
}
|
||||
@@ -53,11 +53,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));
|
||||
@@ -79,7 +79,7 @@ pub(super) fn lower_node(&mut self, def_id: LocalDefId) {
|
||||
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?
|
||||
@@ -101,7 +101,7 @@ pub(super) fn lower_node(&mut self, def_id: LocalDefId) {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'hir> LoweringContext<'_, 'hir> {
|
||||
impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
|
||||
pub(super) fn lower_mod(
|
||||
&mut self,
|
||||
items: &[Box<Item>],
|
||||
@@ -1491,7 +1491,7 @@ fn lower_maybe_coroutine_body(
|
||||
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,
|
||||
@@ -1628,7 +1628,7 @@ pub(crate) fn lower_coroutine_body_with_moved_arguments(
|
||||
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);
|
||||
|
||||
|
||||
@@ -58,7 +58,7 @@
|
||||
use rustc_index::{Idx, IndexSlice, IndexVec};
|
||||
use rustc_macros::extension;
|
||||
use rustc_middle::span_bug;
|
||||
use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
|
||||
use rustc_middle::ty::{DelegationFnSig, DelegationInfo, ResolverAstLowering, TyCtxt};
|
||||
use rustc_session::parse::add_feature_diagnostics;
|
||||
use rustc_span::symbol::{Ident, Symbol, kw, sym};
|
||||
use rustc_span::{DUMMY_SP, DesugaringKind, Span};
|
||||
@@ -87,7 +87,7 @@ macro_rules! arena_vec {
|
||||
mod path;
|
||||
pub mod stability;
|
||||
|
||||
struct LoweringContext<'a, 'hir> {
|
||||
struct LoweringContext<'a, 'hir, R> {
|
||||
tcx: TyCtxt<'hir>,
|
||||
|
||||
// During lowering of delegation we need to access AST of other functions
|
||||
@@ -98,7 +98,7 @@ struct LoweringContext<'a, 'hir> {
|
||||
// will be in AST index.
|
||||
ast_index: &'a IndexSlice<LocalDefId, AstOwner<'a>>,
|
||||
|
||||
resolver: &'a mut ResolverAstLowering<'hir>,
|
||||
resolver: &'a mut R,
|
||||
disambiguator: DisambiguatorState,
|
||||
|
||||
/// Used to allocate HIR nodes.
|
||||
@@ -158,15 +158,14 @@ struct LoweringContext<'a, 'hir> {
|
||||
attribute_parser: AttributeParser<'hir>,
|
||||
}
|
||||
|
||||
impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
impl<'a, 'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'a, 'hir, R> {
|
||||
fn new(
|
||||
tcx: TyCtxt<'hir>,
|
||||
ast_index: &'a IndexSlice<LocalDefId, AstOwner<'a>>,
|
||||
resolver: &'a mut ResolverAstLowering<'hir>,
|
||||
resolver: &'a mut R,
|
||||
) -> Self {
|
||||
let registered_tools = tcx.registered_tools(()).iter().map(|x| x.name).collect();
|
||||
Self {
|
||||
// Pseudo-globals.
|
||||
tcx,
|
||||
ast_index,
|
||||
resolver,
|
||||
@@ -248,9 +247,11 @@ fn lower(&self, span: Span) -> Span {
|
||||
}
|
||||
}
|
||||
|
||||
#[extension(trait ResolverAstLoweringExt)]
|
||||
impl ResolverAstLowering<'_> {
|
||||
fn legacy_const_generic_args(&self, expr: &Expr, tcx: TyCtxt<'_>) -> Option<Vec<usize>> {
|
||||
// LoweringContext now uses this trait for #153489, if #153489 is not merged,
|
||||
// feel free to do whatever you want with this trait.
|
||||
#[extension(trait ResolverAstLoweringExt<'tcx>)]
|
||||
impl<'tcx> ResolverAstLowering<'tcx> {
|
||||
fn legacy_const_generic_args(&self, expr: &Expr, tcx: TyCtxt<'tcx>) -> Option<Vec<usize>> {
|
||||
let ExprKind::Path(None, path) = &expr.kind else {
|
||||
return None;
|
||||
};
|
||||
@@ -261,7 +262,7 @@ fn legacy_const_generic_args(&self, expr: &Expr, tcx: TyCtxt<'_>) -> Option<Vec<
|
||||
return None;
|
||||
}
|
||||
|
||||
let def_id = self.partial_res_map.get(&expr.id)?.full_res()?.opt_def_id()?;
|
||||
let def_id = self.get_partial_res(expr.id)?.full_res()?.opt_def_id()?;
|
||||
|
||||
// We only support cross-crate argument rewriting. Uses
|
||||
// within the same crate should be updated to use the new
|
||||
@@ -304,9 +305,50 @@ fn get_lifetime_res(&self, id: NodeId) -> Option<LifetimeRes> {
|
||||
///
|
||||
/// The extra lifetimes that appear from the parenthesized `Fn`-trait desugaring
|
||||
/// should appear at the enclosing `PolyTraitRef`.
|
||||
fn extra_lifetime_params(&mut self, id: NodeId) -> Vec<(Ident, NodeId, LifetimeRes)> {
|
||||
fn extra_lifetime_params(&self, id: NodeId) -> Vec<(Ident, NodeId, LifetimeRes)> {
|
||||
self.extra_lifetime_params_map.get(&id).cloned().unwrap_or_default()
|
||||
}
|
||||
|
||||
fn delegation_fn_sig(&self, id: LocalDefId) -> Option<&DelegationFnSig> {
|
||||
self.delegation_fn_sigs.get(&id)
|
||||
}
|
||||
|
||||
fn delegation_info(&self, id: LocalDefId) -> Option<&DelegationInfo> {
|
||||
self.delegation_infos.get(&id)
|
||||
}
|
||||
|
||||
fn opt_local_def_id(&self, id: NodeId) -> Option<LocalDefId> {
|
||||
self.node_id_to_def_id.get(&id).copied()
|
||||
}
|
||||
|
||||
fn local_def_id(&self, id: NodeId) -> LocalDefId {
|
||||
self.opt_local_def_id(id).expect("must have def_id")
|
||||
}
|
||||
|
||||
fn lifetime_elision_allowed(&self, id: NodeId) -> bool {
|
||||
self.lifetime_elision_allowed.contains(&id)
|
||||
}
|
||||
|
||||
fn insert_new_def_id(&mut self, node_id: NodeId, def_id: LocalDefId) {
|
||||
self.node_id_to_def_id.insert(node_id, def_id);
|
||||
}
|
||||
|
||||
fn insert_partial_res(&mut self, node_id: NodeId, res: PartialRes) {
|
||||
self.partial_res_map.insert(node_id, res);
|
||||
}
|
||||
|
||||
fn trait_candidates(&self, node_id: NodeId) -> Option<&'tcx [hir::TraitCandidate<'tcx>]> {
|
||||
self.trait_map.get(&node_id).copied()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn next_node_id(&mut self) -> NodeId {
|
||||
let start = self.next_node_id;
|
||||
let next = start.as_u32().checked_add(1).expect("input too large; ran out of NodeIds");
|
||||
self.next_node_id = ast::NodeId::from_u32(next);
|
||||
|
||||
start
|
||||
}
|
||||
}
|
||||
|
||||
/// How relaxed bounds `?Trait` should be treated.
|
||||
@@ -446,42 +488,43 @@ enum TryBlockScope {
|
||||
Heterogeneous(HirId),
|
||||
}
|
||||
|
||||
fn index_crate<'a>(
|
||||
node_id_to_def_id: &NodeMap<LocalDefId>,
|
||||
fn index_crate<'a, 'b>(
|
||||
resolver: &'b impl ResolverAstLoweringExt<'a>,
|
||||
krate: &'a Crate,
|
||||
) -> IndexVec<LocalDefId, AstOwner<'a>> {
|
||||
let mut indexer = Indexer { node_id_to_def_id, index: IndexVec::new() };
|
||||
let mut indexer = Indexer { resolver, index: IndexVec::new() };
|
||||
*indexer.index.ensure_contains_elem(CRATE_DEF_ID, || AstOwner::NonOwner) =
|
||||
AstOwner::Crate(krate);
|
||||
visit::walk_crate(&mut indexer, krate);
|
||||
|
||||
return indexer.index;
|
||||
|
||||
struct Indexer<'s, 'a> {
|
||||
node_id_to_def_id: &'s NodeMap<LocalDefId>,
|
||||
struct Indexer<'a, 'b, R> {
|
||||
resolver: &'b R,
|
||||
index: IndexVec<LocalDefId, AstOwner<'a>>,
|
||||
}
|
||||
|
||||
impl<'a> visit::Visitor<'a> for Indexer<'_, 'a> {
|
||||
impl<'a, 'b, R: ResolverAstLoweringExt<'a>> visit::Visitor<'a> for Indexer<'a, 'b, R> {
|
||||
fn visit_attribute(&mut self, _: &'a Attribute) {
|
||||
// We do not want to lower expressions that appear in attributes,
|
||||
// as they are not accessible to the rest of the HIR.
|
||||
}
|
||||
|
||||
fn visit_item(&mut self, item: &'a ast::Item) {
|
||||
let def_id = self.node_id_to_def_id[&item.id];
|
||||
let def_id = self.resolver.local_def_id(item.id);
|
||||
*self.index.ensure_contains_elem(def_id, || AstOwner::NonOwner) = AstOwner::Item(item);
|
||||
visit::walk_item(self, item)
|
||||
}
|
||||
|
||||
fn visit_assoc_item(&mut self, item: &'a ast::AssocItem, ctxt: visit::AssocCtxt) {
|
||||
let def_id = self.node_id_to_def_id[&item.id];
|
||||
let def_id = self.resolver.local_def_id(item.id);
|
||||
*self.index.ensure_contains_elem(def_id, || AstOwner::NonOwner) =
|
||||
AstOwner::AssocItem(item, ctxt);
|
||||
visit::walk_assoc_item(self, item, ctxt);
|
||||
}
|
||||
|
||||
fn visit_foreign_item(&mut self, item: &'a ast::ForeignItem) {
|
||||
let def_id = self.node_id_to_def_id[&item.id];
|
||||
let def_id = self.resolver.local_def_id(item.id);
|
||||
*self.index.ensure_contains_elem(def_id, || AstOwner::NonOwner) =
|
||||
AstOwner::ForeignItem(item);
|
||||
visit::walk_item(self, item);
|
||||
@@ -521,7 +564,7 @@ pub fn lower_to_hir(tcx: TyCtxt<'_>, (): ()) -> hir::Crate<'_> {
|
||||
tcx.ensure_done().get_lang_items(());
|
||||
let (mut resolver, krate) = tcx.resolver_for_lowering().steal();
|
||||
|
||||
let ast_index = index_crate(&resolver.node_id_to_def_id, &krate);
|
||||
let ast_index = index_crate(&resolver, &krate);
|
||||
let mut owners = IndexVec::from_fn_n(
|
||||
|_| hir::MaybeOwner::Phantom,
|
||||
tcx.definitions_untracked().def_index_count(),
|
||||
@@ -533,6 +576,7 @@ pub fn lower_to_hir(tcx: TyCtxt<'_>, (): ()) -> hir::Crate<'_> {
|
||||
ast_index: &ast_index,
|
||||
owners: &mut owners,
|
||||
};
|
||||
|
||||
for def_id in ast_index.indices() {
|
||||
lowerer.lower_node(def_id);
|
||||
}
|
||||
@@ -579,7 +623,7 @@ enum GenericArgsMode {
|
||||
Silence,
|
||||
}
|
||||
|
||||
impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
|
||||
fn create_def(
|
||||
&mut self,
|
||||
node_id: ast::NodeId,
|
||||
@@ -605,22 +649,19 @@ fn create_def(
|
||||
.def_id();
|
||||
|
||||
debug!("create_def: def_id_to_node_id[{:?}] <-> {:?}", def_id, node_id);
|
||||
self.resolver.node_id_to_def_id.insert(node_id, def_id);
|
||||
self.resolver.insert_new_def_id(node_id, def_id);
|
||||
|
||||
def_id
|
||||
}
|
||||
|
||||
fn next_node_id(&mut self) -> NodeId {
|
||||
let start = self.resolver.next_node_id;
|
||||
let next = start.as_u32().checked_add(1).expect("input too large; ran out of NodeIds");
|
||||
self.resolver.next_node_id = ast::NodeId::from_u32(next);
|
||||
start
|
||||
self.resolver.next_node_id()
|
||||
}
|
||||
|
||||
/// Given the id of some node in the AST, finds the `LocalDefId` associated with it by the name
|
||||
/// resolver (if any).
|
||||
fn opt_local_def_id(&self, node: NodeId) -> Option<LocalDefId> {
|
||||
self.resolver.node_id_to_def_id.get(&node).copied()
|
||||
self.resolver.opt_local_def_id(node)
|
||||
}
|
||||
|
||||
fn local_def_id(&self, node: NodeId) -> LocalDefId {
|
||||
@@ -749,7 +790,7 @@ fn lower_node_id(&mut self, ast_node_id: NodeId) -> HirId {
|
||||
self.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id)));
|
||||
}
|
||||
|
||||
if let Some(&traits) = self.resolver.trait_map.get(&ast_node_id) {
|
||||
if let Some(traits) = self.resolver.trait_candidates(ast_node_id) {
|
||||
self.trait_map.insert(hir_id.local_id, traits);
|
||||
}
|
||||
|
||||
@@ -1766,7 +1807,7 @@ fn lower_fn_decl(
|
||||
inputs,
|
||||
output,
|
||||
c_variadic,
|
||||
lifetime_elision_allowed: self.resolver.lifetime_elision_allowed.contains(&fn_node_id),
|
||||
lifetime_elision_allowed: self.resolver.lifetime_elision_allowed(fn_node_id),
|
||||
implicit_self: decl.inputs.get(0).map_or(hir::ImplicitSelfKind::None, |arg| {
|
||||
let is_mutable_pat = matches!(
|
||||
arg.pat.kind,
|
||||
@@ -2953,7 +2994,10 @@ fn is_empty(&self) -> bool {
|
||||
&& self.parenthesized == hir::GenericArgsParentheses::No
|
||||
}
|
||||
|
||||
fn into_generic_args(self, this: &LoweringContext<'_, 'hir>) -> &'hir hir::GenericArgs<'hir> {
|
||||
fn into_generic_args(
|
||||
self,
|
||||
this: &LoweringContext<'_, 'hir, impl ResolverAstLoweringExt<'hir>>,
|
||||
) -> &'hir hir::GenericArgs<'hir> {
|
||||
let ga = hir::GenericArgs {
|
||||
args: this.arena.alloc_from_iter(self.args),
|
||||
constraints: self.constraints,
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
use super::{ImplTraitContext, LoweringContext, ParamMode, ResolverAstLoweringExt};
|
||||
use crate::{AllowReturnTypeNotation, ImplTraitPosition};
|
||||
|
||||
impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
|
||||
pub(crate) fn lower_pat(&mut self, pattern: &Pat) -> &'hir hir::Pat<'hir> {
|
||||
self.arena.alloc(self.lower_pat_mut(pattern))
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
LifetimeRes, LoweringContext, ParamMode, ResolverAstLoweringExt,
|
||||
};
|
||||
|
||||
impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||
impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
|
||||
#[instrument(level = "trace", skip(self))]
|
||||
pub(crate) fn lower_qpath(
|
||||
&mut self,
|
||||
|
||||
Reference in New Issue
Block a user