2017-06-03 18:26:20 +02:00
|
|
|
//! Lowers the AST to the HIR.
|
|
|
|
//!
|
|
|
|
//! Since the AST and HIR are fairly similar, this is mostly a simple procedure,
|
|
|
|
//! much like a fold. Where lowering involves a bit more work things get more
|
|
|
|
//! interesting and there are some invariants you should know about. These mostly
|
2019-02-08 14:53:55 +01:00
|
|
|
//! concern spans and IDs.
|
2017-06-03 18:26:20 +02:00
|
|
|
//!
|
|
|
|
//! Spans are assigned to AST nodes during parsing and then are modified during
|
|
|
|
//! expansion to indicate the origin of a node and the process it went through
|
2019-02-08 14:53:55 +01:00
|
|
|
//! being expanded. IDs are assigned to AST nodes just before lowering.
|
2017-06-03 18:26:20 +02:00
|
|
|
//!
|
2019-02-08 14:53:55 +01:00
|
|
|
//! For the simpler lowering steps, IDs and spans should be preserved. Unlike
|
2017-06-03 18:26:20 +02:00
|
|
|
//! expansion we do not preserve the process of lowering in the spans, so spans
|
|
|
|
//! should not be modified here. When creating a new node (as opposed to
|
2021-03-06 22:07:38 -08:00
|
|
|
//! "folding" an existing one), create a new ID using `next_id()`.
|
2017-06-03 18:26:20 +02:00
|
|
|
//!
|
2019-02-08 14:53:55 +01:00
|
|
|
//! You must ensure that IDs are unique. That means that you should only use the
|
2019-02-28 22:43:53 +00:00
|
|
|
//! ID from an AST node in a single HIR node (you can assume that AST node-IDs
|
2019-02-08 14:53:55 +01:00
|
|
|
//! are unique). Every new node must have a unique ID. Avoid cloning HIR nodes.
|
|
|
|
//! If you do, you must then set the new node's ID to a fresh one.
|
2017-06-03 18:26:20 +02:00
|
|
|
//!
|
|
|
|
//! Spans are used for error messages and for tools to map semantics back to
|
2019-02-08 14:53:55 +01:00
|
|
|
//! source code. It is therefore not as important with spans as IDs to be strict
|
2017-06-03 18:26:20 +02:00
|
|
|
//! about use (you can't break the compiler by screwing up a span). Obviously, a
|
|
|
|
//! HIR node can only have a single span. But multiple nodes can have the same
|
|
|
|
//! span and spans don't need to be kept in order, etc. Where code is preserved
|
|
|
|
//! by lowering, it should have the same span as in the AST. Where HIR nodes are
|
|
|
|
//! new it is probably best to give a span for the whole AST node being lowered.
|
2021-03-06 22:07:38 -08:00
|
|
|
//! All nodes should have real spans; don't use dummy spans. Tools are likely to
|
2017-06-03 18:26:20 +02:00
|
|
|
//! get confused if the spans from leaf AST nodes occur in multiple places
|
|
|
|
//! in the HIR, especially for multiple identifiers.
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2023-11-13 07:39:17 -05:00
|
|
|
#![allow(internal_features)]
|
|
|
|
#![feature(rustdoc_internals)]
|
|
|
|
#![doc(rust_logo)]
|
2024-01-24 17:30:02 +00:00
|
|
|
#![feature(assert_matches)]
|
2021-01-29 08:31:08 +01:00
|
|
|
#![feature(box_patterns)]
|
2022-08-20 20:40:08 +02:00
|
|
|
#![feature(let_chains)]
|
2019-12-22 18:15:02 +01:00
|
|
|
|
2022-05-20 15:52:56 -03:00
|
|
|
#[macro_use]
|
|
|
|
extern crate tracing;
|
|
|
|
|
2023-09-13 20:09:05 +00:00
|
|
|
use crate::errors::{AssocTyParentheses, AssocTyParenthesesSub, MisplacedImplTrait};
|
2022-08-17 16:58:57 +02:00
|
|
|
|
2023-12-18 21:02:21 +01:00
|
|
|
use rustc_ast::node_id::NodeMap;
|
2022-08-11 21:06:11 +10:00
|
|
|
use rustc_ast::ptr::P;
|
2020-04-27 23:26:11 +05:30
|
|
|
use rustc_ast::{self as ast, *};
|
2020-01-11 17:02:46 +01:00
|
|
|
use rustc_ast_pretty::pprust;
|
2020-01-06 07:03:46 +01:00
|
|
|
use rustc_data_structures::captures::Captures;
|
2021-10-09 19:44:55 +02:00
|
|
|
use rustc_data_structures::fingerprint::Fingerprint;
|
2024-04-04 10:48:47 -04:00
|
|
|
use rustc_data_structures::fx::FxIndexSet;
|
2021-10-21 23:08:57 +02:00
|
|
|
use rustc_data_structures::sorted_map::SortedMap;
|
2021-10-09 19:44:55 +02:00
|
|
|
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
|
2019-05-31 15:50:06 +01:00
|
|
|
use rustc_data_structures::sync::Lrc;
|
2024-02-23 15:34:39 +11:00
|
|
|
use rustc_errors::{DiagArgFromDisplay, DiagCtxt, StashKey};
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir as hir;
|
2021-07-05 22:26:23 +02:00
|
|
|
use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
|
2023-12-18 21:02:21 +01:00
|
|
|
use rustc_hir::def_id::{LocalDefId, LocalDefIdMap, CRATE_DEF_ID, LOCAL_CRATE};
|
2024-02-25 21:46:11 +09:00
|
|
|
use rustc_hir::{
|
2024-03-06 17:24:13 +11:00
|
|
|
ConstArg, GenericArg, HirId, ItemLocalMap, MissingLifetimeKind, ParamName, TraitCandidate,
|
2024-02-25 21:46:11 +09:00
|
|
|
};
|
2023-04-19 10:57:17 +00:00
|
|
|
use rustc_index::{Idx, IndexSlice, IndexVec};
|
2024-02-13 23:49:39 +00:00
|
|
|
use rustc_macros::extension;
|
2023-12-05 23:16:08 +03:00
|
|
|
use rustc_middle::span_bug;
|
2023-12-19 23:16:49 +03:00
|
|
|
use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
|
2023-03-16 22:00:08 +00:00
|
|
|
use rustc_session::parse::{add_feature_diagnostics, feature_err};
|
2020-04-19 13:00:18 +02:00
|
|
|
use rustc_span::symbol::{kw, sym, Ident, Symbol};
|
2023-11-02 14:10:12 +11:00
|
|
|
use rustc_span::{DesugaringKind, Span, DUMMY_SP};
|
2024-01-30 22:03:16 +03:00
|
|
|
use smallvec::{smallvec, SmallVec};
|
2021-07-18 13:03:53 +02:00
|
|
|
use std::collections::hash_map::Entry;
|
2022-11-22 09:17:20 +11:00
|
|
|
use thin_vec::ThinVec;
|
2019-11-11 22:46:56 +01:00
|
|
|
|
2019-12-24 18:57:28 +01:00
|
|
|
macro_rules! arena_vec {
|
2021-09-03 12:36:33 +02:00
|
|
|
($this:expr; $($x:expr),*) => (
|
|
|
|
$this.arena.alloc_from_iter([$($x),*])
|
|
|
|
);
|
2019-12-24 18:57:28 +01:00
|
|
|
}
|
|
|
|
|
2021-04-11 20:51:28 +01:00
|
|
|
mod asm;
|
2021-08-04 13:09:02 -05:00
|
|
|
mod block;
|
2023-11-26 15:57:31 +03:00
|
|
|
mod delegation;
|
2022-08-16 22:28:51 +02:00
|
|
|
mod errors;
|
2019-12-24 18:57:28 +01:00
|
|
|
mod expr;
|
2023-01-11 21:41:13 +01:00
|
|
|
mod format;
|
2021-09-12 03:19:18 +02:00
|
|
|
mod index;
|
2019-12-24 18:57:28 +01:00
|
|
|
mod item;
|
2022-07-29 22:38:07 -03:00
|
|
|
mod lifetime_collector;
|
2020-01-06 05:51:59 +01:00
|
|
|
mod pat;
|
2020-01-06 06:28:43 +01:00
|
|
|
mod path;
|
2019-12-24 18:57:28 +01:00
|
|
|
|
2023-11-22 09:53:07 +11:00
|
|
|
rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
|
2022-10-13 10:13:02 +01:00
|
|
|
|
2021-07-13 18:45:20 +02:00
|
|
|
struct LoweringContext<'a, 'hir> {
|
|
|
|
tcx: TyCtxt<'hir>,
|
2022-06-15 19:42:43 +02:00
|
|
|
resolver: &'a mut ResolverAstLowering,
|
2016-11-24 06:11:31 +02:00
|
|
|
|
2021-03-06 22:07:38 -08:00
|
|
|
/// Used to allocate HIR nodes.
|
2021-07-13 18:45:20 +02:00
|
|
|
arena: &'hir hir::Arena<'hir>,
|
2019-11-28 11:49:29 +01:00
|
|
|
|
2021-10-11 22:36:37 +02:00
|
|
|
/// Bodies inside the owner being lowered.
|
2021-10-21 23:08:57 +02:00
|
|
|
bodies: Vec<(hir::ItemLocalId, &'hir hir::Body<'hir>)>,
|
2021-10-11 22:36:37 +02:00
|
|
|
/// Attributes inside the owner being lowered.
|
2021-10-21 23:08:57 +02:00
|
|
|
attrs: SortedMap<hir::ItemLocalId, &'hir [Attribute]>,
|
2021-07-15 17:41:48 +02:00
|
|
|
/// Collect items that were created by lowering the current owner.
|
2024-02-03 15:50:14 +03:00
|
|
|
children: Vec<(LocalDefId, hir::MaybeOwner<'hir>)>,
|
2017-01-09 17:46:11 +02:00
|
|
|
|
2023-10-19 21:46:28 +00:00
|
|
|
coroutine_kind: Option<hir::CoroutineKind>,
|
2016-12-26 14:34:03 +01:00
|
|
|
|
2020-02-10 17:23:09 +01:00
|
|
|
/// When inside an `async` context, this is the `HirId` of the
|
2023-10-19 21:46:28 +00:00
|
|
|
/// `task_context` local bound to the resume argument of the coroutine.
|
2024-03-06 17:24:13 +11:00
|
|
|
task_context: Option<HirId>,
|
2020-02-10 17:23:09 +01:00
|
|
|
|
2019-05-15 19:47:18 -07:00
|
|
|
/// Used to get the current `fn`'s def span to point to when using `await`
|
|
|
|
/// outside of an `async fn`.
|
2019-05-16 13:17:40 -07:00
|
|
|
current_item: Option<Span>,
|
2019-05-15 19:47:18 -07:00
|
|
|
|
2021-04-01 19:42:27 +02:00
|
|
|
catch_scope: Option<NodeId>,
|
|
|
|
loop_scope: Option<NodeId>,
|
2017-02-15 23:28:59 -08:00
|
|
|
is_in_loop_condition: bool,
|
2017-11-10 12:33:05 -05:00
|
|
|
is_in_trait_impl: bool,
|
2019-03-21 17:55:09 +00:00
|
|
|
is_in_dyn_type: bool,
|
2017-02-15 14:52:27 -08:00
|
|
|
|
2022-09-20 14:11:23 +09:00
|
|
|
current_hir_id_owner: hir::OwnerId,
|
2021-07-16 10:16:23 +02:00
|
|
|
item_local_id_counter: hir::ItemLocalId,
|
2023-12-18 21:02:21 +01:00
|
|
|
trait_map: ItemLocalMap<Box<[TraitCandidate]>>,
|
2019-06-17 00:31:46 +03:00
|
|
|
|
2022-05-31 11:31:57 -03:00
|
|
|
impl_trait_defs: Vec<hir::GenericParam<'hir>>,
|
|
|
|
impl_trait_bounds: Vec<hir::WherePredicate<'hir>>,
|
|
|
|
|
2021-07-16 14:42:26 +02:00
|
|
|
/// NodeIds that are lowered inside the current HIR owner.
|
2023-12-18 21:02:21 +01:00
|
|
|
node_id_to_local_id: NodeMap<hir::ItemLocalId>,
|
2021-07-16 14:42:26 +02:00
|
|
|
|
2023-11-28 12:29:21 +11:00
|
|
|
allow_try_trait: Lrc<[Symbol]>,
|
|
|
|
allow_gen_future: Lrc<[Symbol]>,
|
2023-12-07 17:39:02 +00:00
|
|
|
allow_async_iterator: Lrc<[Symbol]>,
|
2023-12-08 17:00:11 -08:00
|
|
|
allow_for_await: Lrc<[Symbol]>,
|
2024-01-26 17:00:28 +00:00
|
|
|
allow_async_fn_traits: Lrc<[Symbol]>,
|
2022-08-14 18:25:19 +02:00
|
|
|
|
|
|
|
/// Mapping from generics `def_id`s to TAIT generics `def_id`s.
|
|
|
|
/// For each captured lifetime (e.g., 'a), we create a new lifetime parameter that is a generic
|
|
|
|
/// defined on the TAIT, so we have type Foo<'a1> = ... and we establish a mapping in this
|
|
|
|
/// field from the original parameter 'a to the new parameter 'a1.
|
2023-12-18 21:02:21 +01:00
|
|
|
generics_def_id_map: Vec<LocalDefIdMap<LocalDefId>>,
|
2023-07-25 05:58:53 +00:00
|
|
|
|
|
|
|
host_param_id: Option<LocalDefId>,
|
2016-05-02 23:26:18 +00:00
|
|
|
}
|
|
|
|
|
2023-11-28 12:19:17 +11:00
|
|
|
impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
|
|
|
fn new(tcx: TyCtxt<'hir>, resolver: &'a mut ResolverAstLowering) -> Self {
|
|
|
|
Self {
|
|
|
|
// Pseudo-globals.
|
|
|
|
tcx,
|
|
|
|
resolver: resolver,
|
|
|
|
arena: tcx.hir_arena,
|
|
|
|
|
|
|
|
// HirId handling.
|
|
|
|
bodies: Vec::new(),
|
|
|
|
attrs: SortedMap::default(),
|
|
|
|
children: Vec::default(),
|
|
|
|
current_hir_id_owner: hir::CRATE_OWNER_ID,
|
2024-04-03 17:49:59 +03:00
|
|
|
item_local_id_counter: hir::ItemLocalId::ZERO,
|
2023-11-28 12:19:17 +11:00
|
|
|
node_id_to_local_id: Default::default(),
|
|
|
|
trait_map: Default::default(),
|
|
|
|
|
|
|
|
// Lowering state.
|
|
|
|
catch_scope: None,
|
|
|
|
loop_scope: None,
|
|
|
|
is_in_loop_condition: false,
|
|
|
|
is_in_trait_impl: false,
|
|
|
|
is_in_dyn_type: false,
|
|
|
|
coroutine_kind: None,
|
|
|
|
task_context: None,
|
|
|
|
current_item: None,
|
|
|
|
impl_trait_defs: Vec::new(),
|
|
|
|
impl_trait_bounds: Vec::new(),
|
2023-11-28 12:29:21 +11:00
|
|
|
allow_try_trait: [sym::try_trait_v2, sym::yeet_desugar_details].into(),
|
|
|
|
allow_gen_future: if tcx.features().async_fn_track_caller {
|
|
|
|
[sym::gen_future, sym::closure_track_caller].into()
|
2023-11-28 12:19:17 +11:00
|
|
|
} else {
|
2023-11-28 12:29:21 +11:00
|
|
|
[sym::gen_future].into()
|
|
|
|
},
|
2023-12-08 17:00:11 -08:00
|
|
|
allow_for_await: [sym::async_iterator].into(),
|
2024-01-26 17:00:28 +00:00
|
|
|
allow_async_fn_traits: [sym::async_fn_traits].into(),
|
2023-12-08 21:46:08 +00:00
|
|
|
// FIXME(gen_blocks): how does `closure_track_caller`/`async_fn_track_caller`
|
|
|
|
// interact with `gen`/`async gen` blocks
|
2023-12-07 17:39:02 +00:00
|
|
|
allow_async_iterator: [sym::gen_future, sym::async_iterator].into(),
|
2023-11-28 12:19:17 +11:00
|
|
|
generics_def_id_map: Default::default(),
|
|
|
|
host_param_id: None,
|
|
|
|
}
|
|
|
|
}
|
2023-12-18 22:21:37 +11:00
|
|
|
|
|
|
|
pub(crate) fn dcx(&self) -> &'hir DiagCtxt {
|
|
|
|
self.tcx.dcx()
|
|
|
|
}
|
2023-11-28 12:19:17 +11:00
|
|
|
}
|
|
|
|
|
2024-02-14 17:18:56 +00:00
|
|
|
#[extension(trait ResolverAstLoweringExt)]
|
|
|
|
impl ResolverAstLowering {
|
2021-07-05 22:26:23 +02:00
|
|
|
fn legacy_const_generic_args(&self, expr: &Expr) -> Option<Vec<usize>> {
|
|
|
|
if let ExprKind::Path(None, path) = &expr.kind {
|
|
|
|
// Don't perform legacy const generics rewriting if the path already
|
|
|
|
// has generic arguments.
|
|
|
|
if path.segments.last().unwrap().args.is_some() {
|
|
|
|
return None;
|
|
|
|
}
|
2021-09-19 22:17:50 +02:00
|
|
|
|
2022-10-10 19:21:35 +04:00
|
|
|
if let Res::Def(DefKind::Fn, def_id) = self.partial_res_map.get(&expr.id)?.full_res()? {
|
2021-07-05 22:26:23 +02:00
|
|
|
// We only support cross-crate argument rewriting. Uses
|
|
|
|
// within the same crate should be updated to use the new
|
|
|
|
// const generics style.
|
|
|
|
if def_id.is_local() {
|
|
|
|
return None;
|
|
|
|
}
|
2020-06-12 18:13:10 +01:00
|
|
|
|
2021-07-05 22:26:23 +02:00
|
|
|
if let Some(v) = self.legacy_const_generic_args.get(&def_id) {
|
|
|
|
return v.clone();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-20 19:59:29 +01:00
|
|
|
|
2021-07-05 22:26:23 +02:00
|
|
|
None
|
|
|
|
}
|
2020-06-20 19:59:29 +01:00
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
/// Obtains resolution for a `NodeId` with a single resolution.
|
2021-07-05 22:26:23 +02:00
|
|
|
fn get_partial_res(&self, id: NodeId) -> Option<PartialRes> {
|
|
|
|
self.partial_res_map.get(&id).copied()
|
|
|
|
}
|
2020-06-20 19:59:29 +01:00
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
/// Obtains per-namespace resolutions for `use` statement with the given `NodeId`.
|
2021-07-05 22:26:23 +02:00
|
|
|
fn get_import_res(&self, id: NodeId) -> PerNS<Option<Res<NodeId>>> {
|
|
|
|
self.import_res_map.get(&id).copied().unwrap_or_default()
|
|
|
|
}
|
2021-06-22 19:20:25 +02:00
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
/// Obtains resolution for a label with the given `NodeId`.
|
2021-07-05 22:26:23 +02:00
|
|
|
fn get_label_res(&self, id: NodeId) -> Option<NodeId> {
|
|
|
|
self.label_res_map.get(&id).copied()
|
|
|
|
}
|
2021-12-11 19:52:23 +08:00
|
|
|
|
2022-04-07 20:54:13 +02:00
|
|
|
/// Obtains resolution for a lifetime with the given `NodeId`.
|
2021-07-05 22:26:23 +02:00
|
|
|
fn get_lifetime_res(&self, id: NodeId) -> Option<LifetimeRes> {
|
|
|
|
self.lifetimes_res_map.get(&id).copied()
|
|
|
|
}
|
2022-04-07 20:54:13 +02:00
|
|
|
|
2022-04-24 15:49:00 +02:00
|
|
|
/// Obtain the list of lifetimes parameters to add to an item.
|
2022-05-11 22:49:39 +02:00
|
|
|
///
|
|
|
|
/// Extra lifetime parameters should only be added in places that can appear
|
|
|
|
/// as a `binder` in `LifetimeRes`.
|
|
|
|
///
|
|
|
|
/// The extra lifetimes that appear from the parenthesized `Fn`-trait desugaring
|
|
|
|
/// should appear at the enclosing `PolyTraitRef`.
|
2021-07-05 22:26:23 +02:00
|
|
|
fn take_extra_lifetime_params(&mut self, id: NodeId) -> Vec<(Ident, NodeId, LifetimeRes)> {
|
|
|
|
self.extra_lifetime_params_map.remove(&id).unwrap_or_default()
|
|
|
|
}
|
2015-09-25 16:03:28 +12:00
|
|
|
}
|
|
|
|
|
2019-02-28 22:43:53 +00:00
|
|
|
/// Context of `impl Trait` in code, which determines whether it is allowed in an HIR subtree,
|
|
|
|
/// and if so, what meaning it has.
|
2022-05-31 16:47:40 -03:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
2022-05-31 11:31:57 -03:00
|
|
|
enum ImplTraitContext {
|
2017-11-10 12:23:59 -05:00
|
|
|
/// Treat `impl Trait` as shorthand for a new universal generic parameter.
|
|
|
|
/// Example: `fn foo(x: impl Debug)`, where `impl Debug` is conceptually
|
|
|
|
/// equivalent to a fresh universal parameter like `fn foo<T: Debug>(x: T)`.
|
2018-06-19 13:47:53 +02:00
|
|
|
///
|
2018-08-19 03:40:50 +01:00
|
|
|
/// Newly generated parameters should be inserted into the given `Vec`.
|
2022-06-03 20:17:12 +02:00
|
|
|
Universal,
|
2017-11-10 12:23:59 -05:00
|
|
|
|
2019-08-01 00:41:54 +01:00
|
|
|
/// Treat `impl Trait` as shorthand for a new opaque type.
|
2017-11-10 12:23:59 -05:00
|
|
|
/// Example: `fn foo() -> impl Debug`, where `impl Debug` is conceptually
|
2019-08-01 00:41:54 +01:00
|
|
|
/// equivalent to a new opaque type like `type T = impl Debug; fn foo() -> T`.
|
2018-05-22 14:31:56 +02:00
|
|
|
///
|
2024-03-06 18:44:55 +00:00
|
|
|
OpaqueTy {
|
2024-03-06 17:52:53 +00:00
|
|
|
origin: hir::OpaqueTyOrigin,
|
2024-03-06 18:44:55 +00:00
|
|
|
/// Only used to change the lifetime capture rules, since
|
|
|
|
/// RPITIT captures all in scope, RPIT does not.
|
|
|
|
fn_kind: Option<FnDeclKind>,
|
2024-03-06 17:52:53 +00:00
|
|
|
},
|
2022-12-06 17:53:50 -08:00
|
|
|
/// `impl Trait` is unstably accepted in this position.
|
|
|
|
FeatureGated(ImplTraitPosition, Symbol),
|
2017-11-10 12:23:59 -05:00
|
|
|
/// `impl Trait` is not accepted in this position.
|
2018-09-27 22:34:19 +01:00
|
|
|
Disallowed(ImplTraitPosition),
|
|
|
|
}
|
|
|
|
|
2019-03-21 18:40:00 +00:00
|
|
|
/// Position in which `impl Trait` is disallowed.
|
2018-09-27 22:34:19 +01:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
|
|
|
enum ImplTraitPosition {
|
2022-01-11 19:00:34 -08:00
|
|
|
Path,
|
|
|
|
Variable,
|
|
|
|
Trait,
|
|
|
|
Bound,
|
|
|
|
Generic,
|
|
|
|
ExternFnParam,
|
|
|
|
ClosureParam,
|
|
|
|
PointerParam,
|
|
|
|
FnTraitParam,
|
|
|
|
ExternFnReturn,
|
|
|
|
ClosureReturn,
|
|
|
|
PointerReturn,
|
|
|
|
FnTraitReturn,
|
2023-02-14 22:15:32 +00:00
|
|
|
GenericDefault,
|
|
|
|
ConstTy,
|
|
|
|
StaticTy,
|
|
|
|
AssocTy,
|
|
|
|
FieldTy,
|
|
|
|
Cast,
|
|
|
|
ImplSelf,
|
2022-09-11 00:37:49 -07:00
|
|
|
OffsetOf,
|
2017-11-10 12:23:59 -05:00
|
|
|
}
|
|
|
|
|
2022-01-11 19:00:34 -08:00
|
|
|
impl std::fmt::Display for ImplTraitPosition {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
let name = match self {
|
2023-02-22 01:10:52 +00:00
|
|
|
ImplTraitPosition::Path => "paths",
|
2024-01-07 17:11:48 +00:00
|
|
|
ImplTraitPosition::Variable => "the type of variable bindings",
|
2023-02-22 01:10:52 +00:00
|
|
|
ImplTraitPosition::Trait => "traits",
|
|
|
|
ImplTraitPosition::Bound => "bounds",
|
|
|
|
ImplTraitPosition::Generic => "generics",
|
2024-01-07 02:41:28 +00:00
|
|
|
ImplTraitPosition::ExternFnParam => "`extern fn` parameters",
|
|
|
|
ImplTraitPosition::ClosureParam => "closure parameters",
|
|
|
|
ImplTraitPosition::PointerParam => "`fn` pointer parameters",
|
|
|
|
ImplTraitPosition::FnTraitParam => "the parameters of `Fn` trait bounds",
|
2023-02-22 01:10:52 +00:00
|
|
|
ImplTraitPosition::ExternFnReturn => "`extern fn` return types",
|
|
|
|
ImplTraitPosition::ClosureReturn => "closure return types",
|
|
|
|
ImplTraitPosition::PointerReturn => "`fn` pointer return types",
|
2024-01-07 17:11:48 +00:00
|
|
|
ImplTraitPosition::FnTraitReturn => "the return type of `Fn` trait bounds",
|
2023-02-22 01:10:52 +00:00
|
|
|
ImplTraitPosition::GenericDefault => "generic parameter defaults",
|
|
|
|
ImplTraitPosition::ConstTy => "const types",
|
|
|
|
ImplTraitPosition::StaticTy => "static types",
|
|
|
|
ImplTraitPosition::AssocTy => "associated types",
|
|
|
|
ImplTraitPosition::FieldTy => "field types",
|
2024-01-07 02:41:28 +00:00
|
|
|
ImplTraitPosition::Cast => "cast expression types",
|
2023-02-22 01:10:52 +00:00
|
|
|
ImplTraitPosition::ImplSelf => "impl headers",
|
2024-01-07 02:41:28 +00:00
|
|
|
ImplTraitPosition::OffsetOf => "`offset_of!` parameters",
|
2022-01-11 19:00:34 -08:00
|
|
|
};
|
|
|
|
|
2022-12-19 10:31:55 +01:00
|
|
|
write!(f, "{name}")
|
2022-01-11 19:00:34 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-04 23:54:14 +00:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
2022-01-11 19:00:34 -08:00
|
|
|
enum FnDeclKind {
|
|
|
|
Fn,
|
|
|
|
Inherent,
|
|
|
|
ExternFn,
|
|
|
|
Closure,
|
|
|
|
Pointer,
|
|
|
|
Trait,
|
|
|
|
Impl,
|
|
|
|
}
|
|
|
|
|
2021-07-15 01:18:39 +02:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
enum AstOwner<'a> {
|
|
|
|
NonOwner,
|
|
|
|
Crate(&'a ast::Crate),
|
|
|
|
Item(&'a ast::Item),
|
|
|
|
AssocItem(&'a ast::AssocItem, visit::AssocCtxt),
|
|
|
|
ForeignItem(&'a ast::ForeignItem),
|
|
|
|
}
|
|
|
|
|
|
|
|
fn index_crate<'a>(
|
2023-12-18 21:02:21 +01:00
|
|
|
node_id_to_def_id: &NodeMap<LocalDefId>,
|
2021-07-15 01:18:39 +02:00
|
|
|
krate: &'a Crate,
|
|
|
|
) -> IndexVec<LocalDefId, AstOwner<'a>> {
|
2021-07-18 20:09:20 +02:00
|
|
|
let mut indexer = Indexer { node_id_to_def_id, index: IndexVec::new() };
|
2023-04-17 13:14:03 +00:00
|
|
|
*indexer.index.ensure_contains_elem(CRATE_DEF_ID, || AstOwner::NonOwner) =
|
|
|
|
AstOwner::Crate(krate);
|
2021-07-15 01:18:39 +02:00
|
|
|
visit::walk_crate(&mut indexer, krate);
|
|
|
|
return indexer.index;
|
|
|
|
|
|
|
|
struct Indexer<'s, 'a> {
|
2023-12-18 21:02:21 +01:00
|
|
|
node_id_to_def_id: &'s NodeMap<LocalDefId>,
|
2021-07-15 01:18:39 +02:00
|
|
|
index: IndexVec<LocalDefId, AstOwner<'a>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> visit::Visitor<'a> for Indexer<'_, 'a> {
|
|
|
|
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) {
|
2021-07-18 20:09:20 +02:00
|
|
|
let def_id = self.node_id_to_def_id[&item.id];
|
2023-04-17 13:14:03 +00:00
|
|
|
*self.index.ensure_contains_elem(def_id, || AstOwner::NonOwner) = AstOwner::Item(item);
|
2021-07-15 01:18:39 +02:00
|
|
|
visit::walk_item(self, item)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_assoc_item(&mut self, item: &'a ast::AssocItem, ctxt: visit::AssocCtxt) {
|
2021-07-18 20:09:20 +02:00
|
|
|
let def_id = self.node_id_to_def_id[&item.id];
|
2023-04-17 13:14:03 +00:00
|
|
|
*self.index.ensure_contains_elem(def_id, || AstOwner::NonOwner) =
|
|
|
|
AstOwner::AssocItem(item, ctxt);
|
2021-07-15 01:18:39 +02:00
|
|
|
visit::walk_assoc_item(self, item, ctxt);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_foreign_item(&mut self, item: &'a ast::ForeignItem) {
|
2021-07-18 20:09:20 +02:00
|
|
|
let def_id = self.node_id_to_def_id[&item.id];
|
2023-04-17 13:14:03 +00:00
|
|
|
*self.index.ensure_contains_elem(def_id, || AstOwner::NonOwner) =
|
|
|
|
AstOwner::ForeignItem(item);
|
2021-07-15 01:18:39 +02:00
|
|
|
visit::walk_foreign_item(self, item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-14 21:00:02 +02:00
|
|
|
/// Compute the hash for the HIR of the full crate.
|
|
|
|
/// This hash will then be part of the crate_hash which is stored in the metadata.
|
|
|
|
fn compute_hir_hash(
|
2021-07-13 18:45:20 +02:00
|
|
|
tcx: TyCtxt<'_>,
|
2024-02-03 15:50:14 +03:00
|
|
|
owners: &IndexSlice<LocalDefId, hir::MaybeOwner<'_>>,
|
2021-07-14 21:00:02 +02:00
|
|
|
) -> Fingerprint {
|
|
|
|
let mut hir_body_nodes: Vec<_> = owners
|
|
|
|
.iter_enumerated()
|
|
|
|
.filter_map(|(def_id, info)| {
|
|
|
|
let info = info.as_owner()?;
|
2021-07-13 18:45:20 +02:00
|
|
|
let def_path_hash = tcx.hir().def_path_hash(def_id);
|
2021-07-14 21:00:02 +02:00
|
|
|
Some((def_path_hash, info))
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
hir_body_nodes.sort_unstable_by_key(|bn| bn.0);
|
|
|
|
|
2021-07-13 18:45:20 +02:00
|
|
|
tcx.with_stable_hashing_context(|mut hcx| {
|
|
|
|
let mut stable_hasher = StableHasher::new();
|
|
|
|
hir_body_nodes.hash_stable(&mut hcx, &mut stable_hasher);
|
|
|
|
stable_hasher.finish()
|
|
|
|
})
|
2021-07-14 21:00:02 +02:00
|
|
|
}
|
|
|
|
|
2022-12-20 22:10:40 +01:00
|
|
|
pub fn lower_to_hir(tcx: TyCtxt<'_>, (): ()) -> hir::Crate<'_> {
|
2021-07-13 18:45:20 +02:00
|
|
|
let sess = tcx.sess;
|
2023-03-07 13:06:53 +00:00
|
|
|
// Queries that borrow `resolver_for_lowering`.
|
|
|
|
tcx.ensure_with_value().output_filenames(());
|
|
|
|
tcx.ensure_with_value().early_lint_checks(());
|
2023-05-16 18:50:05 +02:00
|
|
|
tcx.ensure_with_value().debugger_visualizers(LOCAL_CRATE);
|
2023-11-23 04:45:42 +00:00
|
|
|
tcx.ensure_with_value().get_lang_items(());
|
2024-02-19 17:25:01 +00:00
|
|
|
let (mut resolver, krate) = tcx.resolver_for_lowering().steal();
|
2019-10-08 14:05:41 +02:00
|
|
|
|
2021-07-13 18:45:20 +02:00
|
|
|
let ast_index = index_crate(&resolver.node_id_to_def_id, &krate);
|
|
|
|
let mut owners = IndexVec::from_fn_n(
|
|
|
|
|_| hir::MaybeOwner::Phantom,
|
|
|
|
tcx.definitions_untracked().def_index_count(),
|
|
|
|
);
|
2021-07-14 21:00:02 +02:00
|
|
|
|
2021-07-15 01:18:39 +02:00
|
|
|
for def_id in ast_index.indices() {
|
2021-07-05 22:26:23 +02:00
|
|
|
item::ItemLowerer {
|
2021-07-13 18:45:20 +02:00
|
|
|
tcx,
|
2022-06-15 19:42:43 +02:00
|
|
|
resolver: &mut resolver,
|
2021-07-05 22:26:23 +02:00
|
|
|
ast_index: &ast_index,
|
|
|
|
owners: &mut owners,
|
|
|
|
}
|
|
|
|
.lower_node(def_id);
|
2021-07-15 01:18:39 +02:00
|
|
|
}
|
2021-07-14 21:00:02 +02:00
|
|
|
|
2021-07-13 18:45:20 +02:00
|
|
|
// Drop AST to free memory
|
2022-08-09 02:14:43 +02:00
|
|
|
drop(ast_index);
|
|
|
|
sess.time("drop_ast", || drop(krate));
|
2021-07-13 18:45:20 +02:00
|
|
|
|
2023-03-03 17:02:11 +11:00
|
|
|
// Don't hash unless necessary, because it's expensive.
|
|
|
|
let opt_hir_hash =
|
2023-08-08 18:28:20 +08:00
|
|
|
if tcx.needs_crate_hash() { Some(compute_hir_hash(tcx, &owners)) } else { None };
|
2023-03-03 17:02:11 +11:00
|
|
|
hir::Crate { owners, opt_hir_hash }
|
2016-05-10 05:29:13 +00:00
|
|
|
}
|
2015-09-28 15:00:15 +13:00
|
|
|
|
2022-05-20 15:52:56 -03:00
|
|
|
#[derive(Copy, Clone, PartialEq, Debug)]
|
2016-10-17 06:02:23 +03:00
|
|
|
enum ParamMode {
|
|
|
|
/// Any path in a type context.
|
|
|
|
Explicit,
|
2019-06-08 21:35:02 -07:00
|
|
|
/// Path in a type definition, where the anonymous lifetime `'_` is not allowed.
|
|
|
|
ExplicitNamed,
|
2016-10-17 06:02:23 +03:00
|
|
|
/// The `module::Type` in `module::Type::method` in an expression.
|
2018-03-21 04:24:27 -04:00
|
|
|
Optional,
|
2016-10-17 06:02:23 +03:00
|
|
|
}
|
|
|
|
|
2017-07-29 01:13:40 +03:00
|
|
|
enum ParenthesizedGenericArgs {
|
2023-03-03 23:25:54 +00:00
|
|
|
ParenSugar,
|
2017-07-29 01:13:40 +03:00
|
|
|
Err,
|
|
|
|
}
|
|
|
|
|
2019-11-28 11:49:29 +01:00
|
|
|
impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
2021-07-05 22:26:23 +02:00
|
|
|
fn create_def(
|
|
|
|
&mut self,
|
|
|
|
parent: LocalDefId,
|
|
|
|
node_id: ast::NodeId,
|
2023-12-03 12:29:59 +03:00
|
|
|
name: Symbol,
|
2023-11-21 23:40:23 +03:00
|
|
|
def_kind: DefKind,
|
2022-12-01 10:33:28 +00:00
|
|
|
span: Span,
|
2021-07-05 22:26:23 +02:00
|
|
|
) -> LocalDefId {
|
2022-04-02 16:13:01 +02:00
|
|
|
debug_assert_ne!(node_id, ast::DUMMY_NODE_ID);
|
2021-07-05 22:26:23 +02:00
|
|
|
assert!(
|
2021-07-18 20:09:20 +02:00
|
|
|
self.opt_local_def_id(node_id).is_none(),
|
2023-12-03 12:29:59 +03:00
|
|
|
"adding a def'n for node-id {:?} and def kind {:?} but a previous def'n exists: {:?}",
|
2021-07-05 22:26:23 +02:00
|
|
|
node_id,
|
2023-12-03 12:29:59 +03:00
|
|
|
def_kind,
|
2021-07-13 18:45:20 +02:00
|
|
|
self.tcx.hir().def_key(self.local_def_id(node_id)),
|
2021-07-05 22:26:23 +02:00
|
|
|
);
|
|
|
|
|
2023-12-03 12:29:59 +03:00
|
|
|
let def_id = self.tcx.at(span).create_def(parent, name, def_kind).def_id();
|
2021-07-05 22:26:23 +02:00
|
|
|
|
2022-04-02 16:13:01 +02:00
|
|
|
debug!("create_def: def_id_to_node_id[{:?}] <-> {:?}", def_id, node_id);
|
|
|
|
self.resolver.node_id_to_def_id.insert(node_id, def_id);
|
2021-07-05 22:26:23 +02:00
|
|
|
|
|
|
|
def_id
|
|
|
|
}
|
|
|
|
|
2021-07-18 20:09:20 +02:00
|
|
|
fn next_node_id(&mut self) -> NodeId {
|
2022-06-15 19:42:43 +02:00
|
|
|
let start = self.resolver.next_node_id;
|
2022-06-15 19:41:41 +02:00
|
|
|
let next = start.as_u32().checked_add(1).expect("input too large; ran out of NodeIds");
|
2022-06-15 19:42:43 +02:00
|
|
|
self.resolver.next_node_id = ast::NodeId::from_u32(next);
|
2022-06-15 19:41:41 +02:00
|
|
|
start
|
2021-07-18 20:09:20 +02:00
|
|
|
}
|
|
|
|
|
2022-10-19 16:49:21 -03:00
|
|
|
/// Given the id of some node in the AST, finds the `LocalDefId` associated with it by the name
|
|
|
|
/// resolver (if any).
|
|
|
|
fn orig_opt_local_def_id(&self, node: NodeId) -> Option<LocalDefId> {
|
2023-11-03 17:18:19 +01:00
|
|
|
self.resolver.node_id_to_def_id.get(&node).copied()
|
2022-10-19 16:49:21 -03:00
|
|
|
}
|
|
|
|
|
2022-08-03 17:26:38 -03:00
|
|
|
/// Given the id of some node in the AST, finds the `LocalDefId` associated with it by the name
|
|
|
|
/// resolver (if any), after applying any remapping from `get_remapped_def_id`.
|
|
|
|
///
|
2022-08-04 12:47:58 -03:00
|
|
|
/// For example, in a function like `fn foo<'a>(x: &'a u32)`,
|
|
|
|
/// invoking with the id from the `ast::Lifetime` node found inside
|
|
|
|
/// the `&'a u32` type would return the `LocalDefId` of the
|
|
|
|
/// `'a` parameter declared on `foo`.
|
2022-08-03 17:26:38 -03:00
|
|
|
///
|
2022-08-04 12:47:58 -03:00
|
|
|
/// This function also applies remapping from `get_remapped_def_id`.
|
|
|
|
/// These are used when synthesizing opaque types from `-> impl Trait` return types and so forth.
|
|
|
|
/// For example, in a function like `fn foo<'a>() -> impl Debug + 'a`,
|
|
|
|
/// we would create an opaque type `type FooReturn<'a1> = impl Debug + 'a1`.
|
|
|
|
/// When lowering the `Debug + 'a` bounds, we add a remapping to map `'a` to `'a1`.
|
2021-07-18 20:09:20 +02:00
|
|
|
fn opt_local_def_id(&self, node: NodeId) -> Option<LocalDefId> {
|
2022-10-19 16:49:21 -03:00
|
|
|
self.orig_opt_local_def_id(node).map(|local_def_id| self.get_remapped_def_id(local_def_id))
|
2021-07-18 20:09:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn local_def_id(&self, node: NodeId) -> LocalDefId {
|
2022-12-19 10:31:55 +01:00
|
|
|
self.opt_local_def_id(node).unwrap_or_else(|| panic!("no entry for node id: `{node:?}`"))
|
2021-07-18 20:09:20 +02:00
|
|
|
}
|
|
|
|
|
2022-08-14 18:25:19 +02:00
|
|
|
/// Get the previously recorded `to` local def id given the `from` local def id, obtained using
|
|
|
|
/// `generics_def_id_map` field.
|
2022-10-18 14:05:17 -03:00
|
|
|
fn get_remapped_def_id(&self, local_def_id: LocalDefId) -> LocalDefId {
|
2022-08-14 18:25:19 +02:00
|
|
|
// `generics_def_id_map` is a stack of mappings. As we go deeper in impl traits nesting we
|
2022-10-18 14:05:17 -03:00
|
|
|
// push new mappings, so we first need to get the latest (innermost) mappings, hence `iter().rev()`.
|
2022-08-14 18:25:19 +02:00
|
|
|
//
|
|
|
|
// Consider:
|
|
|
|
//
|
|
|
|
// `fn test<'a, 'b>() -> impl Trait<&'a u8, Ty = impl Sized + 'b> {}`
|
|
|
|
//
|
|
|
|
// We would end with a generics_def_id_map like:
|
|
|
|
//
|
|
|
|
// `[[fn#'b -> impl_trait#'b], [fn#'b -> impl_sized#'b]]`
|
|
|
|
//
|
2022-10-18 14:05:17 -03:00
|
|
|
// for the opaque type generated on `impl Sized + 'b`, we want the result to be: impl_sized#'b.
|
|
|
|
// So, if we were trying to find first from the start (outermost) would give the wrong result, impl_trait#'b.
|
|
|
|
self.generics_def_id_map
|
|
|
|
.iter()
|
|
|
|
.rev()
|
2023-11-03 17:18:19 +01:00
|
|
|
.find_map(|map| map.get(&local_def_id).copied())
|
2022-10-18 14:05:17 -03:00
|
|
|
.unwrap_or(local_def_id)
|
2022-08-14 18:25:19 +02:00
|
|
|
}
|
|
|
|
|
2022-05-02 20:32:10 +02:00
|
|
|
/// Freshen the `LoweringContext` and ready it to lower a nested item.
|
|
|
|
/// The lowered item is registered into `self.children`.
|
|
|
|
///
|
|
|
|
/// This function sets up `HirId` lowering infrastructure,
|
|
|
|
/// and stashes the shared mutable state to avoid pollution by the closure.
|
2022-05-20 15:52:56 -03:00
|
|
|
#[instrument(level = "debug", skip(self, f))]
|
2021-07-16 10:16:23 +02:00
|
|
|
fn with_hir_id_owner(
|
|
|
|
&mut self,
|
|
|
|
owner: NodeId,
|
|
|
|
f: impl FnOnce(&mut Self) -> hir::OwnerNode<'hir>,
|
2021-07-15 16:40:41 +02:00
|
|
|
) {
|
2021-07-18 20:09:20 +02:00
|
|
|
let def_id = self.local_def_id(owner);
|
2017-03-14 15:50:40 +01:00
|
|
|
|
2021-07-16 14:42:26 +02:00
|
|
|
let current_attrs = std::mem::take(&mut self.attrs);
|
|
|
|
let current_bodies = std::mem::take(&mut self.bodies);
|
2021-07-18 13:03:53 +02:00
|
|
|
let current_node_ids = std::mem::take(&mut self.node_id_to_local_id);
|
2022-02-19 19:43:18 -03:00
|
|
|
let current_trait_map = std::mem::take(&mut self.trait_map);
|
2022-09-20 14:11:23 +09:00
|
|
|
let current_owner =
|
|
|
|
std::mem::replace(&mut self.current_hir_id_owner, hir::OwnerId { def_id });
|
2021-07-16 10:16:23 +02:00
|
|
|
let current_local_counter =
|
|
|
|
std::mem::replace(&mut self.item_local_id_counter, hir::ItemLocalId::new(1));
|
2022-05-31 11:48:12 -03:00
|
|
|
let current_impl_trait_defs = std::mem::take(&mut self.impl_trait_defs);
|
|
|
|
let current_impl_trait_bounds = std::mem::take(&mut self.impl_trait_bounds);
|
2022-05-02 20:32:10 +02:00
|
|
|
|
|
|
|
// Do not reset `next_node_id` and `node_id_to_def_id`:
|
|
|
|
// we want `f` to be able to refer to the `LocalDefId`s that the caller created.
|
|
|
|
// and the caller to refer to some of the subdefinitions' nodes' `LocalDefId`s.
|
2021-07-14 23:50:53 +02:00
|
|
|
|
2021-07-16 14:42:26 +02:00
|
|
|
// Always allocate the first `HirId` for the owner itself.
|
2024-04-03 17:49:59 +03:00
|
|
|
let _old = self.node_id_to_local_id.insert(owner, hir::ItemLocalId::ZERO);
|
2021-07-16 14:42:26 +02:00
|
|
|
debug_assert_eq!(_old, None);
|
|
|
|
|
2021-07-16 10:16:23 +02:00
|
|
|
let item = f(self);
|
2022-09-20 14:11:23 +09:00
|
|
|
debug_assert_eq!(def_id, item.def_id().def_id);
|
2022-06-01 14:43:16 -03:00
|
|
|
// `f` should have consumed all the elements in these vectors when constructing `item`.
|
2022-05-31 11:48:12 -03:00
|
|
|
debug_assert!(self.impl_trait_defs.is_empty());
|
|
|
|
debug_assert!(self.impl_trait_bounds.is_empty());
|
2021-07-16 14:42:26 +02:00
|
|
|
let info = self.make_owner_info(item);
|
2017-03-14 15:50:40 +01:00
|
|
|
|
2021-07-16 14:42:26 +02:00
|
|
|
self.attrs = current_attrs;
|
|
|
|
self.bodies = current_bodies;
|
2021-07-18 13:03:53 +02:00
|
|
|
self.node_id_to_local_id = current_node_ids;
|
2022-02-19 19:43:18 -03:00
|
|
|
self.trait_map = current_trait_map;
|
2021-07-16 10:16:23 +02:00
|
|
|
self.current_hir_id_owner = current_owner;
|
|
|
|
self.item_local_id_counter = current_local_counter;
|
2022-05-31 11:48:12 -03:00
|
|
|
self.impl_trait_defs = current_impl_trait_defs;
|
|
|
|
self.impl_trait_bounds = current_impl_trait_bounds;
|
2017-03-14 15:50:40 +01:00
|
|
|
|
2022-12-18 12:45:56 +01:00
|
|
|
debug_assert!(!self.children.iter().any(|(id, _)| id == &def_id));
|
2022-11-12 19:07:33 +01:00
|
|
|
self.children.push((def_id, hir::MaybeOwner::Owner(info)));
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
|
2022-08-03 17:32:24 -03:00
|
|
|
/// Installs the remapping `remap` in scope while `f` is being executed.
|
|
|
|
/// This causes references to the `LocalDefId` keys to be changed to
|
|
|
|
/// refer to the values instead.
|
|
|
|
///
|
|
|
|
/// The remapping is used when one piece of AST expands to multiple
|
|
|
|
/// pieces of HIR. For example, the function `fn foo<'a>(...) -> impl Debug + 'a`,
|
|
|
|
/// expands to both a function definition (`foo`) and a TAIT for the return value,
|
|
|
|
/// both of which have a lifetime parameter `'a`. The remapping allows us to
|
|
|
|
/// rewrite the `'a` in the return value to refer to the
|
|
|
|
/// `'a` declared on the TAIT, instead of the function.
|
2022-08-02 20:01:40 -03:00
|
|
|
fn with_remapping<R>(
|
|
|
|
&mut self,
|
2023-12-18 21:02:21 +01:00
|
|
|
remap: LocalDefIdMap<LocalDefId>,
|
2022-08-02 20:01:40 -03:00
|
|
|
f: impl FnOnce(&mut Self) -> R,
|
|
|
|
) -> R {
|
2022-08-14 18:25:19 +02:00
|
|
|
self.generics_def_id_map.push(remap);
|
2022-08-02 20:01:40 -03:00
|
|
|
let res = f(self);
|
2022-08-14 18:25:19 +02:00
|
|
|
self.generics_def_id_map.pop();
|
2022-08-02 20:01:40 -03:00
|
|
|
res
|
|
|
|
}
|
|
|
|
|
2021-07-15 17:41:48 +02:00
|
|
|
fn make_owner_info(&mut self, node: hir::OwnerNode<'hir>) -> &'hir hir::OwnerInfo<'hir> {
|
2021-07-16 14:42:26 +02:00
|
|
|
let attrs = std::mem::take(&mut self.attrs);
|
2021-10-21 23:08:57 +02:00
|
|
|
let mut bodies = std::mem::take(&mut self.bodies);
|
2021-07-15 17:41:48 +02:00
|
|
|
let trait_map = std::mem::take(&mut self.trait_map);
|
2021-07-16 14:42:26 +02:00
|
|
|
|
|
|
|
#[cfg(debug_assertions)]
|
2021-10-21 23:08:57 +02:00
|
|
|
for (id, attrs) in attrs.iter() {
|
2021-07-16 14:42:26 +02:00
|
|
|
// Verify that we do not store empty slices in the map.
|
|
|
|
if attrs.is_empty() {
|
|
|
|
panic!("Stored empty attributes for {:?}", id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-21 23:08:57 +02:00
|
|
|
bodies.sort_by_key(|(k, _)| *k);
|
|
|
|
let bodies = SortedMap::from_presorted_elements(bodies);
|
2023-03-03 17:02:11 +11:00
|
|
|
|
|
|
|
// Don't hash unless necessary, because it's expensive.
|
2024-03-14 22:45:57 +03:00
|
|
|
let (opt_hash_including_bodies, attrs_hash) =
|
|
|
|
self.tcx.hash_owner_nodes(node, &bodies, &attrs);
|
2024-01-20 15:21:27 +03:00
|
|
|
let num_nodes = self.item_local_id_counter.as_usize();
|
|
|
|
let (nodes, parenting) = index::index_hir(self.tcx, node, &bodies, num_nodes);
|
2023-03-03 17:02:11 +11:00
|
|
|
let nodes = hir::OwnerNodes { opt_hash_including_bodies, nodes, bodies };
|
|
|
|
let attrs = hir::AttributeMap { map: attrs, opt_hash: attrs_hash };
|
2021-10-09 19:44:55 +02:00
|
|
|
|
2021-07-15 17:41:48 +02:00
|
|
|
self.arena.alloc(hir::OwnerInfo { nodes, parenting, attrs, trait_map })
|
2021-10-09 19:44:55 +02:00
|
|
|
}
|
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
/// This method allocates a new `HirId` for the given `NodeId` and stores it in
|
|
|
|
/// the `LoweringContext`'s `NodeId => HirId` map.
|
|
|
|
/// Take care not to call this method if the resulting `HirId` is then not
|
2017-03-14 15:50:40 +01:00
|
|
|
/// actually used in the HIR, as that would trigger an assertion in the
|
2018-11-27 02:59:49 +00:00
|
|
|
/// `HirIdValidator` later on, which makes sure that all `NodeId`s got mapped
|
|
|
|
/// properly. Calling the method twice with the same `NodeId` is fine though.
|
2022-09-06 17:24:36 -03:00
|
|
|
#[instrument(level = "debug", skip(self), ret)]
|
2024-03-06 17:24:13 +11:00
|
|
|
fn lower_node_id(&mut self, ast_node_id: NodeId) -> HirId {
|
2021-07-14 23:50:53 +02:00
|
|
|
assert_ne!(ast_node_id, DUMMY_NODE_ID);
|
2017-03-14 15:50:40 +01:00
|
|
|
|
2022-02-19 14:47:50 -03:00
|
|
|
match self.node_id_to_local_id.entry(ast_node_id) {
|
2024-03-06 17:24:13 +11:00
|
|
|
Entry::Occupied(o) => HirId { owner: self.current_hir_id_owner, local_id: *o.get() },
|
2021-07-18 13:03:53 +02:00
|
|
|
Entry::Vacant(v) => {
|
|
|
|
// Generate a new `HirId`.
|
2022-02-19 14:47:50 -03:00
|
|
|
let owner = self.current_hir_id_owner;
|
2021-07-18 13:03:53 +02:00
|
|
|
let local_id = self.item_local_id_counter;
|
2024-03-06 17:24:13 +11:00
|
|
|
let hir_id = HirId { owner, local_id };
|
2022-02-19 14:47:50 -03:00
|
|
|
|
2021-07-18 13:03:53 +02:00
|
|
|
v.insert(local_id);
|
2022-02-19 14:47:50 -03:00
|
|
|
self.item_local_id_counter.increment_by(1);
|
|
|
|
|
2024-04-03 17:49:59 +03:00
|
|
|
assert_ne!(local_id, hir::ItemLocalId::ZERO);
|
2021-07-18 20:09:20 +02:00
|
|
|
if let Some(def_id) = self.opt_local_def_id(ast_node_id) {
|
2022-11-12 19:07:33 +01:00
|
|
|
self.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id)));
|
2022-02-22 09:37:47 -03:00
|
|
|
}
|
2022-02-19 19:43:18 -03:00
|
|
|
|
2021-07-05 22:26:23 +02:00
|
|
|
if let Some(traits) = self.resolver.trait_map.remove(&ast_node_id) {
|
2022-02-22 09:37:47 -03:00
|
|
|
self.trait_map.insert(hir_id.local_id, traits.into_boxed_slice());
|
2022-02-19 14:47:50 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
hir_id
|
2021-07-18 13:03:53 +02:00
|
|
|
}
|
2022-02-19 14:47:50 -03:00
|
|
|
}
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
|
2022-04-02 16:13:20 +02:00
|
|
|
/// Generate a new `HirId` without a backing `NodeId`.
|
2022-09-06 17:24:36 -03:00
|
|
|
#[instrument(level = "debug", skip(self), ret)]
|
2024-03-06 17:24:13 +11:00
|
|
|
fn next_id(&mut self) -> HirId {
|
2022-04-02 16:13:20 +02:00
|
|
|
let owner = self.current_hir_id_owner;
|
|
|
|
let local_id = self.item_local_id_counter;
|
2024-04-03 17:49:59 +03:00
|
|
|
assert_ne!(local_id, hir::ItemLocalId::ZERO);
|
2022-04-02 16:13:20 +02:00
|
|
|
self.item_local_id_counter.increment_by(1);
|
2024-03-06 17:24:13 +11:00
|
|
|
HirId { owner, local_id }
|
2016-06-22 01:16:56 +00:00
|
|
|
}
|
|
|
|
|
2022-05-20 15:52:56 -03:00
|
|
|
#[instrument(level = "trace", skip(self))]
|
2019-04-20 19:36:05 +03:00
|
|
|
fn lower_res(&mut self, res: Res<NodeId>) -> Res {
|
2021-07-18 13:03:53 +02:00
|
|
|
let res: Result<Res, ()> = res.apply_id(|id| {
|
|
|
|
let owner = self.current_hir_id_owner;
|
|
|
|
let local_id = self.node_id_to_local_id.get(&id).copied().ok_or(())?;
|
2024-03-06 17:24:13 +11:00
|
|
|
Ok(HirId { owner, local_id })
|
2021-07-18 13:03:53 +02:00
|
|
|
});
|
2022-05-20 15:52:56 -03:00
|
|
|
trace!(?res);
|
|
|
|
|
2021-07-18 13:03:53 +02:00
|
|
|
// We may fail to find a HirId when the Res points to a Local from an enclosing HIR owner.
|
|
|
|
// This can happen when trying to lower the return type `x` in erroneous code like
|
|
|
|
// async fn foo(x: u8) -> x {}
|
|
|
|
// In that case, `x` is lowered as a function parameter, and the return type is lowered as
|
2022-03-30 01:39:38 -04:00
|
|
|
// an opaque type as a synthesized HIR owner.
|
2021-07-18 13:03:53 +02:00
|
|
|
res.unwrap_or(Res::Err)
|
2019-04-03 09:07:45 +02:00
|
|
|
}
|
|
|
|
|
2019-04-20 19:36:05 +03:00
|
|
|
fn expect_full_res(&mut self, id: NodeId) -> Res<NodeId> {
|
2022-10-10 19:21:35 +04:00
|
|
|
self.resolver.get_partial_res(id).map_or(Res::Err, |pr| pr.expect_full_res())
|
2016-11-25 13:21:19 +02:00
|
|
|
}
|
|
|
|
|
2024-01-30 22:03:16 +03:00
|
|
|
fn lower_import_res(&mut self, id: NodeId, span: Span) -> SmallVec<[Res; 3]> {
|
|
|
|
let res = self.resolver.get_import_res(id).present_items();
|
|
|
|
let res: SmallVec<_> = res.map(|res| self.lower_res(res)).collect();
|
|
|
|
if res.is_empty() {
|
|
|
|
self.dcx().span_delayed_bug(span, "no resolution for an import");
|
|
|
|
return smallvec![Res::Err];
|
|
|
|
}
|
|
|
|
res
|
2018-06-13 11:44:06 -05:00
|
|
|
}
|
|
|
|
|
2023-12-23 04:02:17 +00:00
|
|
|
fn make_lang_item_qpath(&mut self, lang_item: hir::LangItem, span: Span) -> hir::QPath<'hir> {
|
|
|
|
hir::QPath::Resolved(None, self.make_lang_item_path(lang_item, span, None))
|
|
|
|
}
|
|
|
|
|
2023-11-23 04:55:03 +00:00
|
|
|
fn make_lang_item_path(
|
|
|
|
&mut self,
|
|
|
|
lang_item: hir::LangItem,
|
|
|
|
span: Span,
|
|
|
|
args: Option<&'hir hir::GenericArgs<'hir>>,
|
|
|
|
) -> &'hir hir::Path<'hir> {
|
|
|
|
let def_id = self.tcx.require_lang_item(lang_item, Some(span));
|
|
|
|
let def_kind = self.tcx.def_kind(def_id);
|
|
|
|
let res = Res::Def(def_kind, def_id);
|
|
|
|
self.arena.alloc(hir::Path {
|
|
|
|
span,
|
|
|
|
res,
|
|
|
|
segments: self.arena.alloc_from_iter([hir::PathSegment {
|
|
|
|
ident: Ident::new(lang_item.name(), span),
|
|
|
|
hir_id: self.next_id(),
|
|
|
|
res,
|
|
|
|
args,
|
2023-12-23 04:02:17 +00:00
|
|
|
infer_args: args.is_none(),
|
2023-11-23 04:55:03 +00:00
|
|
|
}]),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-05-31 15:50:06 +01:00
|
|
|
/// Reuses the span but adds information like the kind of the desugaring and features that are
|
|
|
|
/// allowed inside this span.
|
|
|
|
fn mark_span_with_reason(
|
|
|
|
&self,
|
2019-06-19 01:08:45 +03:00
|
|
|
reason: DesugaringKind,
|
2019-05-31 15:50:06 +01:00
|
|
|
span: Span,
|
|
|
|
allow_internal_unstable: Option<Lrc<[Symbol]>>,
|
|
|
|
) -> Span {
|
2021-07-13 18:45:20 +02:00
|
|
|
self.tcx.with_stable_hashing_context(|hcx| {
|
2022-05-02 20:32:17 +02:00
|
|
|
span.mark_with_reason(allow_internal_unstable, reason, self.tcx.sess.edition(), hcx)
|
2021-07-13 18:45:20 +02:00
|
|
|
})
|
2019-05-31 15:50:06 +01:00
|
|
|
}
|
|
|
|
|
2021-08-21 00:29:08 +03:00
|
|
|
/// Intercept all spans entering HIR.
|
2021-04-18 21:28:23 +02:00
|
|
|
/// Mark a span as relative to the current owning item.
|
2021-08-21 00:29:08 +03:00
|
|
|
fn lower_span(&self, span: Span) -> Span {
|
2023-09-07 20:13:57 +00:00
|
|
|
if self.tcx.sess.opts.incremental.is_some() {
|
2022-09-20 14:11:23 +09:00
|
|
|
span.with_parent(Some(self.current_hir_id_owner.def_id))
|
2021-04-18 21:28:23 +02:00
|
|
|
} else {
|
|
|
|
// Do not make spans relative when not using incremental compilation.
|
|
|
|
span
|
|
|
|
}
|
2021-08-21 00:29:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn lower_ident(&self, ident: Ident) -> Ident {
|
|
|
|
Ident::new(ident.name, self.lower_span(ident.span))
|
|
|
|
}
|
|
|
|
|
2019-03-13 17:42:23 -07:00
|
|
|
/// Converts a lifetime into a new generic parameter.
|
2022-08-31 13:01:10 +00:00
|
|
|
#[instrument(level = "debug", skip(self))]
|
2022-04-24 15:49:00 +02:00
|
|
|
fn lifetime_res_to_generic_param(
|
2019-03-13 17:42:23 -07:00
|
|
|
&mut self,
|
2022-04-24 15:49:00 +02:00
|
|
|
ident: Ident,
|
2022-03-01 21:08:26 +01:00
|
|
|
node_id: NodeId,
|
2022-04-24 15:49:00 +02:00
|
|
|
res: LifetimeRes,
|
2023-02-28 06:26:48 +00:00
|
|
|
source: hir::GenericParamSource,
|
2022-04-24 15:49:00 +02:00
|
|
|
) -> Option<hir::GenericParam<'hir>> {
|
|
|
|
let (name, kind) = match res {
|
|
|
|
LifetimeRes::Param { .. } => {
|
|
|
|
(hir::ParamName::Plain(ident), hir::LifetimeParamKind::Explicit)
|
|
|
|
}
|
2024-02-25 21:46:11 +09:00
|
|
|
LifetimeRes::Fresh { param, kind, .. } => {
|
2022-06-06 09:02:24 +02:00
|
|
|
// Late resolution delegates to us the creation of the `LocalDefId`.
|
|
|
|
let _def_id = self.create_def(
|
2022-09-20 14:11:23 +09:00
|
|
|
self.current_hir_id_owner.def_id,
|
2022-06-06 09:02:24 +02:00
|
|
|
param,
|
2023-12-03 12:29:59 +03:00
|
|
|
kw::UnderscoreLifetime,
|
2023-11-21 23:40:23 +03:00
|
|
|
DefKind::LifetimeParam,
|
2022-12-01 10:33:28 +00:00
|
|
|
ident.span,
|
2022-06-06 09:02:24 +02:00
|
|
|
);
|
|
|
|
debug!(?_def_id);
|
|
|
|
|
2024-02-25 21:46:11 +09:00
|
|
|
(hir::ParamName::Fresh, hir::LifetimeParamKind::Elided(kind))
|
2022-06-06 09:02:24 +02:00
|
|
|
}
|
2022-04-24 15:49:00 +02:00
|
|
|
LifetimeRes::Static | LifetimeRes::Error => return None,
|
|
|
|
res => panic!(
|
|
|
|
"Unexpected lifetime resolution {:?} for {:?} at {:?}",
|
|
|
|
res, ident, ident.span
|
|
|
|
),
|
|
|
|
};
|
2022-03-01 21:08:26 +01:00
|
|
|
let hir_id = self.lower_node_id(node_id);
|
2022-11-06 18:26:36 +00:00
|
|
|
let def_id = self.local_def_id(node_id);
|
2022-04-24 15:49:00 +02:00
|
|
|
Some(hir::GenericParam {
|
2022-03-01 21:08:26 +01:00
|
|
|
hir_id,
|
2022-11-06 18:26:36 +00:00
|
|
|
def_id,
|
2022-04-24 15:49:00 +02:00
|
|
|
name,
|
|
|
|
span: self.lower_span(ident.span),
|
2019-03-13 17:42:23 -07:00
|
|
|
pure_wrt_drop: false,
|
2022-04-24 15:49:00 +02:00
|
|
|
kind: hir::GenericParamKind::Lifetime { kind },
|
2022-04-28 21:59:41 +02:00
|
|
|
colon_span: None,
|
2023-02-28 06:26:48 +00:00
|
|
|
source,
|
2022-04-24 15:49:00 +02:00
|
|
|
})
|
2017-11-16 22:59:45 -08:00
|
|
|
}
|
|
|
|
|
2022-08-03 19:09:14 -03:00
|
|
|
/// Lowers a lifetime binder that defines `generic_params`, returning the corresponding HIR
|
|
|
|
/// nodes. The returned list includes any "extra" lifetime parameters that were added by the
|
|
|
|
/// name resolver owing to lifetime elision; this also populates the resolver's node-id->def-id
|
|
|
|
/// map, so that later calls to `opt_node_id_to_def_id` that refer to these extra lifetime
|
|
|
|
/// parameters will be successful.
|
2022-09-14 17:14:36 -03:00
|
|
|
#[instrument(level = "debug", skip(self))]
|
2022-04-20 20:13:42 +02:00
|
|
|
#[inline]
|
2022-09-14 17:14:36 -03:00
|
|
|
fn lower_lifetime_binder(
|
2022-05-11 22:49:39 +02:00
|
|
|
&mut self,
|
|
|
|
binder: NodeId,
|
|
|
|
generic_params: &[GenericParam],
|
2022-09-14 17:14:36 -03:00
|
|
|
) -> &'hir [hir::GenericParam<'hir>] {
|
2024-01-01 19:29:27 +01:00
|
|
|
let mut generic_params: Vec<_> = self
|
|
|
|
.lower_generic_params_mut(generic_params, hir::GenericParamSource::Binder)
|
2023-02-28 06:26:48 +00:00
|
|
|
.collect();
|
2022-05-11 22:49:39 +02:00
|
|
|
let extra_lifetimes = self.resolver.take_extra_lifetime_params(binder);
|
|
|
|
debug!(?extra_lifetimes);
|
2022-09-14 17:14:36 -03:00
|
|
|
generic_params.extend(extra_lifetimes.into_iter().filter_map(|(ident, node_id, res)| {
|
2023-02-28 06:26:48 +00:00
|
|
|
self.lifetime_res_to_generic_param(ident, node_id, res, hir::GenericParamSource::Binder)
|
2022-09-14 17:14:36 -03:00
|
|
|
}));
|
2022-05-11 22:49:39 +02:00
|
|
|
let generic_params = self.arena.alloc_from_iter(generic_params);
|
|
|
|
debug!(?generic_params);
|
|
|
|
|
2022-09-14 17:14:36 -03:00
|
|
|
generic_params
|
2022-04-20 20:13:42 +02:00
|
|
|
}
|
|
|
|
|
2020-01-06 05:32:17 +01:00
|
|
|
fn with_dyn_type_scope<T>(&mut self, in_scope: bool, f: impl FnOnce(&mut Self) -> T) -> T {
|
2019-03-21 17:55:09 +00:00
|
|
|
let was_in_dyn_type = self.is_in_dyn_type;
|
|
|
|
self.is_in_dyn_type = in_scope;
|
|
|
|
|
|
|
|
let result = f(self);
|
|
|
|
|
|
|
|
self.is_in_dyn_type = was_in_dyn_type;
|
|
|
|
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2023-11-28 19:06:23 +00:00
|
|
|
fn with_new_scopes<T>(&mut self, scope_span: Span, f: impl FnOnce(&mut Self) -> T) -> T {
|
|
|
|
let current_item = self.current_item;
|
|
|
|
self.current_item = Some(scope_span);
|
|
|
|
|
2017-02-15 23:28:59 -08:00
|
|
|
let was_in_loop_condition = self.is_in_loop_condition;
|
|
|
|
self.is_in_loop_condition = false;
|
|
|
|
|
2021-04-01 19:42:27 +02:00
|
|
|
let catch_scope = self.catch_scope.take();
|
|
|
|
let loop_scope = self.loop_scope.take();
|
2018-08-19 03:40:50 +01:00
|
|
|
let ret = f(self);
|
2021-04-01 19:42:27 +02:00
|
|
|
self.catch_scope = catch_scope;
|
|
|
|
self.loop_scope = loop_scope;
|
2017-02-15 23:28:59 -08:00
|
|
|
|
|
|
|
self.is_in_loop_condition = was_in_loop_condition;
|
|
|
|
|
2023-11-28 19:06:23 +00:00
|
|
|
self.current_item = current_item;
|
|
|
|
|
2018-08-19 03:40:50 +01:00
|
|
|
ret
|
2017-02-15 14:52:27 -08:00
|
|
|
}
|
|
|
|
|
2024-03-06 17:24:13 +11:00
|
|
|
fn lower_attrs(&mut self, id: HirId, attrs: &[Attribute]) -> Option<&'hir [Attribute]> {
|
2021-01-24 17:14:17 +01:00
|
|
|
if attrs.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
2021-07-16 14:42:26 +02:00
|
|
|
debug_assert_eq!(id.owner, self.current_hir_id_owner);
|
2021-01-24 17:14:17 +01:00
|
|
|
let ret = self.arena.alloc_from_iter(attrs.iter().map(|a| self.lower_attr(a)));
|
|
|
|
debug_assert!(!ret.is_empty());
|
2021-07-16 14:42:26 +02:00
|
|
|
self.attrs.insert(id.local_id, ret);
|
2021-01-24 17:14:17 +01:00
|
|
|
Some(ret)
|
|
|
|
}
|
2017-09-15 08:28:34 -07:00
|
|
|
}
|
|
|
|
|
2020-11-27 17:41:05 +01:00
|
|
|
fn lower_attr(&self, attr: &Attribute) -> Attribute {
|
2018-10-11 21:15:18 +13:00
|
|
|
// Note that we explicitly do not walk the path. Since we don't really
|
|
|
|
// lower attributes (we use the AST version) there is nowhere to keep
|
2018-11-27 02:59:49 +00:00
|
|
|
// the `HirId`s. We don't actually need HIR version of attributes anyway.
|
2020-11-05 20:27:48 +03:00
|
|
|
// Tokens are also not needed after macro expansion and parsing.
|
2019-10-24 06:33:12 +11:00
|
|
|
let kind = match attr.kind {
|
2022-08-11 21:06:11 +10:00
|
|
|
AttrKind::Normal(ref normal) => AttrKind::Normal(P(NormalAttr {
|
|
|
|
item: AttrItem {
|
|
|
|
path: normal.item.path.clone(),
|
2022-11-18 11:24:21 +11:00
|
|
|
args: self.lower_attr_args(&normal.item.args),
|
2020-11-05 20:27:48 +03:00
|
|
|
tokens: None,
|
|
|
|
},
|
2022-08-11 21:06:11 +10:00
|
|
|
tokens: None,
|
|
|
|
})),
|
2020-07-21 22:16:19 +03:00
|
|
|
AttrKind::DocComment(comment_kind, data) => AttrKind::DocComment(comment_kind, data),
|
2019-10-24 06:33:12 +11:00
|
|
|
};
|
|
|
|
|
2021-08-21 00:29:08 +03:00
|
|
|
Attribute { kind, id: attr.id, style: attr.style, span: self.lower_span(attr.span) }
|
2017-09-15 08:28:34 -07:00
|
|
|
}
|
|
|
|
|
2024-03-06 17:24:13 +11:00
|
|
|
fn alias_attrs(&mut self, id: HirId, target_id: HirId) {
|
2021-07-16 14:42:26 +02:00
|
|
|
debug_assert_eq!(id.owner, self.current_hir_id_owner);
|
|
|
|
debug_assert_eq!(target_id.owner, self.current_hir_id_owner);
|
|
|
|
if let Some(&a) = self.attrs.get(&target_id.local_id) {
|
2021-01-24 17:14:17 +01:00
|
|
|
debug_assert!(!a.is_empty());
|
2021-07-16 14:42:26 +02:00
|
|
|
self.attrs.insert(id.local_id, a);
|
2021-01-24 17:14:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-18 11:24:21 +11:00
|
|
|
fn lower_attr_args(&self, args: &AttrArgs) -> AttrArgs {
|
2022-11-22 15:37:54 +00:00
|
|
|
match args {
|
2022-11-18 11:24:21 +11:00
|
|
|
AttrArgs::Empty => AttrArgs::Empty,
|
2022-11-22 15:37:54 +00:00
|
|
|
AttrArgs::Delimited(args) => AttrArgs::Delimited(self.lower_delim_args(args)),
|
2020-11-23 01:43:55 -05:00
|
|
|
// This is an inert key-value attribute - it will never be visible to macros
|
Overhaul `MacArgs::Eq`.
The value in `MacArgs::Eq` is currently represented as a `Token`.
Because of `TokenKind::Interpolated`, `Token` can be either a token or
an arbitrary AST fragment. In practice, a `MacArgs::Eq` starts out as a
literal or macro call AST fragment, and then is later lowered to a
literal token. But this is very non-obvious. `Token` is a much more
general type than what is needed.
This commit restricts things, by introducing a new type `MacArgsEqKind`
that is either an AST expression (pre-lowering) or an AST literal
(post-lowering). The downside is that the code is a bit more verbose in
a few places. The benefit is that makes it much clearer what the
possibilities are (though also shorter in some other places). Also, it
removes one use of `TokenKind::Interpolated`, taking us a step closer to
removing that variant, which will let us make `Token` impl `Copy` and
remove many "handle Interpolated" code paths in the parser.
Things to note:
- Error messages have improved. Messages like this:
```
unexpected token: `"bug" + "found"`
```
now say "unexpected expression", which makes more sense. Although
arbitrary expressions can exist within tokens thanks to
`TokenKind::Interpolated`, that's not obvious to anyone who doesn't
know compiler internals.
- In `parse_mac_args_common`, we no longer need to collect tokens for
the value expression.
2022-04-29 06:52:01 +10:00
|
|
|
// after it gets lowered to HIR. Therefore, we can extract literals to handle
|
|
|
|
// nonterminals in `#[doc]` (e.g. `#[doc = $e]`).
|
2022-11-22 15:37:54 +00:00
|
|
|
AttrArgs::Eq(eq_span, AttrArgsEq::Ast(expr)) => {
|
Overhaul `MacArgs::Eq`.
The value in `MacArgs::Eq` is currently represented as a `Token`.
Because of `TokenKind::Interpolated`, `Token` can be either a token or
an arbitrary AST fragment. In practice, a `MacArgs::Eq` starts out as a
literal or macro call AST fragment, and then is later lowered to a
literal token. But this is very non-obvious. `Token` is a much more
general type than what is needed.
This commit restricts things, by introducing a new type `MacArgsEqKind`
that is either an AST expression (pre-lowering) or an AST literal
(post-lowering). The downside is that the code is a bit more verbose in
a few places. The benefit is that makes it much clearer what the
possibilities are (though also shorter in some other places). Also, it
removes one use of `TokenKind::Interpolated`, taking us a step closer to
removing that variant, which will let us make `Token` impl `Copy` and
remove many "handle Interpolated" code paths in the parser.
Things to note:
- Error messages have improved. Messages like this:
```
unexpected token: `"bug" + "found"`
```
now say "unexpected expression", which makes more sense. Although
arbitrary expressions can exist within tokens thanks to
`TokenKind::Interpolated`, that's not obvious to anyone who doesn't
know compiler internals.
- In `parse_mac_args_common`, we no longer need to collect tokens for
the value expression.
2022-04-29 06:52:01 +10:00
|
|
|
// In valid code the value always ends up as a single literal. Otherwise, a dummy
|
|
|
|
// literal suffices because the error is handled elsewhere.
|
2022-11-23 15:39:42 +11:00
|
|
|
let lit = if let ExprKind::Lit(token_lit) = expr.kind
|
|
|
|
&& let Ok(lit) = MetaItemLit::from_token_lit(token_lit, expr.span)
|
2022-11-24 13:47:57 +11:00
|
|
|
{
|
|
|
|
lit
|
2022-04-28 14:12:40 +10:00
|
|
|
} else {
|
2024-02-14 20:12:05 +11:00
|
|
|
let guar = self.dcx().has_errors().unwrap();
|
2022-11-23 15:39:42 +11:00
|
|
|
MetaItemLit {
|
2022-11-29 13:36:00 +11:00
|
|
|
symbol: kw::Empty,
|
|
|
|
suffix: None,
|
2024-02-14 20:12:05 +11:00
|
|
|
kind: LitKind::Err(guar),
|
Overhaul `MacArgs::Eq`.
The value in `MacArgs::Eq` is currently represented as a `Token`.
Because of `TokenKind::Interpolated`, `Token` can be either a token or
an arbitrary AST fragment. In practice, a `MacArgs::Eq` starts out as a
literal or macro call AST fragment, and then is later lowered to a
literal token. But this is very non-obvious. `Token` is a much more
general type than what is needed.
This commit restricts things, by introducing a new type `MacArgsEqKind`
that is either an AST expression (pre-lowering) or an AST literal
(post-lowering). The downside is that the code is a bit more verbose in
a few places. The benefit is that makes it much clearer what the
possibilities are (though also shorter in some other places). Also, it
removes one use of `TokenKind::Interpolated`, taking us a step closer to
removing that variant, which will let us make `Token` impl `Copy` and
remove many "handle Interpolated" code paths in the parser.
Things to note:
- Error messages have improved. Messages like this:
```
unexpected token: `"bug" + "found"`
```
now say "unexpected expression", which makes more sense. Although
arbitrary expressions can exist within tokens thanks to
`TokenKind::Interpolated`, that's not obvious to anyone who doesn't
know compiler internals.
- In `parse_mac_args_common`, we no longer need to collect tokens for
the value expression.
2022-04-29 06:52:01 +10:00
|
|
|
span: DUMMY_SP,
|
2020-12-19 23:38:22 +03:00
|
|
|
}
|
2022-04-28 14:12:40 +10:00
|
|
|
};
|
2022-11-22 15:37:54 +00:00
|
|
|
AttrArgs::Eq(*eq_span, AttrArgsEq::Hir(lit))
|
Overhaul `MacArgs::Eq`.
The value in `MacArgs::Eq` is currently represented as a `Token`.
Because of `TokenKind::Interpolated`, `Token` can be either a token or
an arbitrary AST fragment. In practice, a `MacArgs::Eq` starts out as a
literal or macro call AST fragment, and then is later lowered to a
literal token. But this is very non-obvious. `Token` is a much more
general type than what is needed.
This commit restricts things, by introducing a new type `MacArgsEqKind`
that is either an AST expression (pre-lowering) or an AST literal
(post-lowering). The downside is that the code is a bit more verbose in
a few places. The benefit is that makes it much clearer what the
possibilities are (though also shorter in some other places). Also, it
removes one use of `TokenKind::Interpolated`, taking us a step closer to
removing that variant, which will let us make `Token` impl `Copy` and
remove many "handle Interpolated" code paths in the parser.
Things to note:
- Error messages have improved. Messages like this:
```
unexpected token: `"bug" + "found"`
```
now say "unexpected expression", which makes more sense. Although
arbitrary expressions can exist within tokens thanks to
`TokenKind::Interpolated`, that's not obvious to anyone who doesn't
know compiler internals.
- In `parse_mac_args_common`, we no longer need to collect tokens for
the value expression.
2022-04-29 06:52:01 +10:00
|
|
|
}
|
2022-11-22 15:37:54 +00:00
|
|
|
AttrArgs::Eq(_, AttrArgsEq::Hir(lit)) => {
|
Overhaul `MacArgs::Eq`.
The value in `MacArgs::Eq` is currently represented as a `Token`.
Because of `TokenKind::Interpolated`, `Token` can be either a token or
an arbitrary AST fragment. In practice, a `MacArgs::Eq` starts out as a
literal or macro call AST fragment, and then is later lowered to a
literal token. But this is very non-obvious. `Token` is a much more
general type than what is needed.
This commit restricts things, by introducing a new type `MacArgsEqKind`
that is either an AST expression (pre-lowering) or an AST literal
(post-lowering). The downside is that the code is a bit more verbose in
a few places. The benefit is that makes it much clearer what the
possibilities are (though also shorter in some other places). Also, it
removes one use of `TokenKind::Interpolated`, taking us a step closer to
removing that variant, which will let us make `Token` impl `Copy` and
remove many "handle Interpolated" code paths in the parser.
Things to note:
- Error messages have improved. Messages like this:
```
unexpected token: `"bug" + "found"`
```
now say "unexpected expression", which makes more sense. Although
arbitrary expressions can exist within tokens thanks to
`TokenKind::Interpolated`, that's not obvious to anyone who doesn't
know compiler internals.
- In `parse_mac_args_common`, we no longer need to collect tokens for
the value expression.
2022-04-29 06:52:01 +10:00
|
|
|
unreachable!("in literal form when lowering mac args eq: {:?}", lit)
|
2020-12-19 23:38:22 +03:00
|
|
|
}
|
2017-09-15 08:28:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-18 11:24:21 +11:00
|
|
|
fn lower_delim_args(&self, args: &DelimArgs) -> DelimArgs {
|
|
|
|
DelimArgs { dspan: args.dspan, delim: args.delim, tokens: args.tokens.flattened() }
|
|
|
|
}
|
|
|
|
|
2019-05-08 15:56:39 -04:00
|
|
|
/// Given an associated type constraint like one of these:
|
|
|
|
///
|
2022-04-15 15:04:34 -07:00
|
|
|
/// ```ignore (illustrative)
|
2019-05-08 15:56:39 -04:00
|
|
|
/// T: Iterator<Item: Debug>
|
|
|
|
/// ^^^^^^^^^^^
|
|
|
|
/// T: Iterator<Item = Debug>
|
|
|
|
/// ^^^^^^^^^^^^
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// returns a `hir::TypeBinding` representing `Item`.
|
2022-05-20 15:52:56 -03:00
|
|
|
#[instrument(level = "debug", skip(self))]
|
2019-08-19 12:24:06 -07:00
|
|
|
fn lower_assoc_ty_constraint(
|
|
|
|
&mut self,
|
2021-07-30 08:56:45 +00:00
|
|
|
constraint: &AssocConstraint,
|
2024-02-07 19:27:44 +00:00
|
|
|
itctx: ImplTraitContext,
|
2019-11-30 17:46:46 +01:00
|
|
|
) -> hir::TypeBinding<'hir> {
|
2019-08-19 12:24:06 -07:00
|
|
|
debug!("lower_assoc_ty_constraint(constraint={:?}, itctx={:?})", constraint, itctx);
|
2020-11-30 09:24:54 +01:00
|
|
|
// lower generic arguments of identifier in constraint
|
2022-11-22 15:37:54 +00:00
|
|
|
let gen_args = if let Some(gen_args) = &constraint.gen_args {
|
2020-11-30 09:24:54 +01:00
|
|
|
let gen_args_ctor = match gen_args {
|
2022-11-22 15:37:54 +00:00
|
|
|
GenericArgs::AngleBracketed(data) => {
|
2022-05-31 16:46:15 -03:00
|
|
|
self.lower_angle_bracketed_parameter_data(data, ParamMode::Explicit, itctx).0
|
2020-11-30 09:24:54 +01:00
|
|
|
}
|
2022-11-22 15:37:54 +00:00
|
|
|
GenericArgs::Parenthesized(data) => {
|
2023-04-10 22:16:17 +00:00
|
|
|
if data.inputs.is_empty() && matches!(data.output, FnRetTy::Default(..)) {
|
|
|
|
let parenthesized = if self.tcx.features().return_type_notation {
|
|
|
|
hir::GenericArgsParentheses::ReturnTypeNotation
|
|
|
|
} else {
|
|
|
|
self.emit_bad_parenthesized_trait_in_assoc_ty(data);
|
|
|
|
hir::GenericArgsParentheses::No
|
|
|
|
};
|
|
|
|
GenericArgsCtor {
|
|
|
|
args: Default::default(),
|
|
|
|
bindings: &[],
|
|
|
|
parenthesized,
|
|
|
|
span: data.inputs_span,
|
|
|
|
}
|
|
|
|
} else if let Some(first_char) = constraint.ident.as_str().chars().next()
|
|
|
|
&& first_char.is_ascii_lowercase()
|
2023-03-16 22:00:08 +00:00
|
|
|
{
|
|
|
|
let mut err = if !data.inputs.is_empty() {
|
2023-12-18 22:21:37 +11:00
|
|
|
self.dcx().create_err(errors::BadReturnTypeNotation::Inputs {
|
2023-03-16 22:00:08 +00:00
|
|
|
span: data.inputs_span,
|
|
|
|
})
|
|
|
|
} else if let FnRetTy::Ty(ty) = &data.output {
|
2023-12-18 22:21:37 +11:00
|
|
|
self.dcx().create_err(errors::BadReturnTypeNotation::Output {
|
2023-03-16 22:00:08 +00:00
|
|
|
span: data.inputs_span.shrink_to_hi().to(ty.span),
|
|
|
|
})
|
|
|
|
} else {
|
2023-04-10 22:16:17 +00:00
|
|
|
unreachable!("inputs are empty and return type is not provided")
|
2023-03-16 22:00:08 +00:00
|
|
|
};
|
|
|
|
if !self.tcx.features().return_type_notation
|
|
|
|
&& self.tcx.sess.is_nightly_build()
|
|
|
|
{
|
|
|
|
add_feature_diagnostics(
|
|
|
|
&mut err,
|
2024-01-10 00:37:30 -05:00
|
|
|
&self.tcx.sess,
|
2023-03-16 22:00:08 +00:00
|
|
|
sym::return_type_notation,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
err.emit();
|
|
|
|
GenericArgsCtor {
|
|
|
|
args: Default::default(),
|
|
|
|
bindings: &[],
|
|
|
|
parenthesized: hir::GenericArgsParentheses::ReturnTypeNotation,
|
|
|
|
span: data.span,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.emit_bad_parenthesized_trait_in_assoc_ty(data);
|
|
|
|
// FIXME(return_type_notation): we could issue a feature error
|
|
|
|
// if the parens are empty and there's no return type.
|
|
|
|
self.lower_angle_bracketed_parameter_data(
|
|
|
|
&data.as_angle_bracketed_args(),
|
|
|
|
ParamMode::Explicit,
|
|
|
|
itctx,
|
|
|
|
)
|
|
|
|
.0
|
|
|
|
}
|
2020-11-30 09:24:54 +01:00
|
|
|
}
|
|
|
|
};
|
2021-08-21 00:29:08 +03:00
|
|
|
gen_args_ctor.into_generic_args(self)
|
2020-11-30 09:24:54 +01:00
|
|
|
} else {
|
|
|
|
self.arena.alloc(hir::GenericArgs::none())
|
|
|
|
};
|
2022-11-22 15:37:54 +00:00
|
|
|
let kind = match &constraint.kind {
|
|
|
|
AssocConstraintKind::Equality { term } => {
|
2022-01-08 09:28:12 +00:00
|
|
|
let term = match term {
|
2022-11-22 15:37:54 +00:00
|
|
|
Term::Ty(ty) => self.lower_ty(ty, itctx).into(),
|
|
|
|
Term::Const(c) => self.lower_anon_const(c).into(),
|
2022-01-08 09:28:12 +00:00
|
|
|
};
|
|
|
|
hir::TypeBindingKind::Equality { term }
|
2019-12-24 17:38:22 -05:00
|
|
|
}
|
2022-11-22 15:37:54 +00:00
|
|
|
AssocConstraintKind::Bound { bounds } => {
|
2024-02-06 19:48:16 +00:00
|
|
|
// Disallow ATB in dyn types
|
|
|
|
if self.is_in_dyn_type {
|
|
|
|
let suggestion = match itctx {
|
2024-03-06 18:44:55 +00:00
|
|
|
ImplTraitContext::OpaqueTy { .. } | ImplTraitContext::Universal => {
|
2024-02-06 19:48:16 +00:00
|
|
|
let bound_end_span = constraint
|
|
|
|
.gen_args
|
|
|
|
.as_ref()
|
|
|
|
.map_or(constraint.ident.span, |args| args.span());
|
|
|
|
if bound_end_span.eq_ctxt(constraint.span) {
|
|
|
|
Some(self.tcx.sess.source_map().next_point(bound_end_span))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
let guar = self.dcx().emit_err(errors::MisplacedAssocTyBinding {
|
|
|
|
span: constraint.span,
|
|
|
|
suggestion,
|
|
|
|
});
|
|
|
|
let err_ty =
|
|
|
|
&*self.arena.alloc(self.ty(constraint.span, hir::TyKind::Err(guar)));
|
|
|
|
hir::TypeBindingKind::Equality { term: err_ty.into() }
|
|
|
|
} else {
|
|
|
|
// Desugar `AssocTy: Bounds` into a type binding where the
|
|
|
|
// later desugars into a trait predicate.
|
|
|
|
let bounds = self.lower_param_bounds(bounds, itctx);
|
2019-03-21 17:55:09 +00:00
|
|
|
|
2024-02-06 19:48:16 +00:00
|
|
|
hir::TypeBindingKind::Constraint { bounds }
|
2019-03-21 17:55:09 +00:00
|
|
|
}
|
2019-02-28 22:43:53 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
hir::TypeBinding {
|
2019-08-19 12:24:06 -07:00
|
|
|
hir_id: self.lower_node_id(constraint.id),
|
2021-08-21 00:29:08 +03:00
|
|
|
ident: self.lower_ident(constraint.ident),
|
2020-11-30 09:24:54 +01:00
|
|
|
gen_args,
|
2019-05-08 15:57:06 -04:00
|
|
|
kind,
|
2021-08-21 00:29:08 +03:00
|
|
|
span: self.lower_span(constraint.span),
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2022-06-15 01:06:40 +01:00
|
|
|
fn emit_bad_parenthesized_trait_in_assoc_ty(&self, data: &ParenthesizedArgs) {
|
2022-06-02 18:22:25 +01:00
|
|
|
// Suggest removing empty parentheses: "Trait()" -> "Trait"
|
2022-08-17 16:58:57 +02:00
|
|
|
let sub = if data.inputs.is_empty() {
|
2022-06-02 18:22:25 +01:00
|
|
|
let parentheses_span =
|
|
|
|
data.inputs_span.shrink_to_lo().to(data.inputs_span.shrink_to_hi());
|
2022-08-17 16:58:57 +02:00
|
|
|
AssocTyParenthesesSub::Empty { parentheses_span }
|
2022-06-02 18:22:25 +01:00
|
|
|
}
|
|
|
|
// Suggest replacing parentheses with angle brackets `Trait(params...)` to `Trait<params...>`
|
|
|
|
else {
|
|
|
|
// Start of parameters to the 1st argument
|
|
|
|
let open_param = data.inputs_span.shrink_to_lo().to(data
|
|
|
|
.inputs
|
|
|
|
.first()
|
|
|
|
.unwrap()
|
|
|
|
.span
|
|
|
|
.shrink_to_lo());
|
|
|
|
// End of last argument to end of parameters
|
|
|
|
let close_param =
|
|
|
|
data.inputs.last().unwrap().span.shrink_to_hi().to(data.inputs_span.shrink_to_hi());
|
2022-08-17 16:58:57 +02:00
|
|
|
AssocTyParenthesesSub::NotEmpty { open_param, close_param }
|
|
|
|
};
|
2023-12-18 22:21:37 +11:00
|
|
|
self.dcx().emit_err(AssocTyParentheses { span: data.span, sub });
|
2022-06-02 18:22:25 +01:00
|
|
|
}
|
|
|
|
|
2022-05-20 15:52:56 -03:00
|
|
|
#[instrument(level = "debug", skip(self))]
|
2019-11-18 14:30:01 -05:00
|
|
|
fn lower_generic_arg(
|
|
|
|
&mut self,
|
|
|
|
arg: &ast::GenericArg,
|
2024-02-07 19:27:44 +00:00
|
|
|
itctx: ImplTraitContext,
|
2019-11-30 17:46:46 +01:00
|
|
|
) -> hir::GenericArg<'hir> {
|
2018-05-27 16:56:01 +01:00
|
|
|
match arg {
|
2023-11-21 20:07:32 +01:00
|
|
|
ast::GenericArg::Lifetime(lt) => GenericArg::Lifetime(self.lower_lifetime(lt)),
|
2019-11-18 14:30:01 -05:00
|
|
|
ast::GenericArg::Type(ty) => {
|
2022-11-22 15:37:54 +00:00
|
|
|
match &ty.kind {
|
2022-05-02 20:32:17 +02:00
|
|
|
TyKind::Infer if self.tcx.features().generic_arg_infer => {
|
2021-05-06 15:33:44 +00:00
|
|
|
return GenericArg::Infer(hir::InferArg {
|
2021-08-21 00:29:08 +03:00
|
|
|
hir_id: self.lower_node_id(ty.id),
|
|
|
|
span: self.lower_span(ty.span),
|
2021-05-06 15:33:44 +00:00
|
|
|
});
|
2021-04-24 21:41:57 +00:00
|
|
|
}
|
|
|
|
// We parse const arguments as path types as we cannot distinguish them during
|
|
|
|
// parsing. We try to resolve that ambiguity by attempting resolution in both the
|
|
|
|
// type and value namespaces. If we resolved the path in the value namespace, we
|
|
|
|
// transform it into a generic const argument.
|
2023-05-05 21:31:00 +01:00
|
|
|
TyKind::Path(None, path) => {
|
2022-10-10 19:21:35 +04:00
|
|
|
if let Some(res) = self
|
|
|
|
.resolver
|
|
|
|
.get_partial_res(ty.id)
|
|
|
|
.and_then(|partial_res| partial_res.full_res())
|
|
|
|
{
|
2023-05-05 21:31:00 +01:00
|
|
|
if !res.matches_ns(Namespace::TypeNS)
|
|
|
|
&& path.is_potential_trivial_const_arg()
|
|
|
|
{
|
2021-04-24 21:41:57 +00:00
|
|
|
debug!(
|
|
|
|
"lower_generic_arg: Lowering type argument as const argument: {:?}",
|
|
|
|
ty,
|
|
|
|
);
|
|
|
|
|
2021-08-22 14:46:15 +02:00
|
|
|
// Construct an AnonConst where the expr is the "ty"'s path.
|
2021-04-24 21:41:57 +00:00
|
|
|
|
2021-07-16 10:16:23 +02:00
|
|
|
let parent_def_id = self.current_hir_id_owner;
|
2021-07-18 20:09:20 +02:00
|
|
|
let node_id = self.next_node_id();
|
2022-12-01 10:33:28 +00:00
|
|
|
let span = self.lower_span(ty.span);
|
2021-04-24 21:41:57 +00:00
|
|
|
|
|
|
|
// Add a definition for the in-band const def.
|
2022-11-06 19:17:57 +00:00
|
|
|
let def_id = self.create_def(
|
2022-09-20 14:11:23 +09:00
|
|
|
parent_def_id.def_id,
|
|
|
|
node_id,
|
2023-12-03 12:29:59 +03:00
|
|
|
kw::Empty,
|
2023-11-21 23:40:23 +03:00
|
|
|
DefKind::AnonConst,
|
2022-12-01 10:33:28 +00:00
|
|
|
span,
|
2022-09-20 14:11:23 +09:00
|
|
|
);
|
2021-04-24 21:41:57 +00:00
|
|
|
|
|
|
|
let path_expr = Expr {
|
|
|
|
id: ty.id,
|
2023-05-05 21:31:00 +01:00
|
|
|
kind: ExprKind::Path(None, path.clone()),
|
2021-08-21 00:29:08 +03:00
|
|
|
span,
|
2021-04-24 21:41:57 +00:00
|
|
|
attrs: AttrVec::new(),
|
|
|
|
tokens: None,
|
|
|
|
};
|
|
|
|
|
2023-11-28 19:06:23 +00:00
|
|
|
let ct = self.with_new_scopes(span, |this| hir::AnonConst {
|
2022-11-06 19:17:57 +00:00
|
|
|
def_id,
|
2021-04-24 21:41:57 +00:00
|
|
|
hir_id: this.lower_node_id(node_id),
|
|
|
|
body: this.lower_const_body(path_expr.span, Some(&path_expr)),
|
|
|
|
});
|
2023-10-25 15:28:23 +00:00
|
|
|
return GenericArg::Const(ConstArg {
|
|
|
|
value: ct,
|
|
|
|
span,
|
|
|
|
is_desugared_from_effects: false,
|
|
|
|
});
|
2021-04-24 21:41:57 +00:00
|
|
|
}
|
2019-11-18 14:30:01 -05:00
|
|
|
}
|
|
|
|
}
|
2021-04-24 21:41:57 +00:00
|
|
|
_ => {}
|
2019-11-18 14:30:01 -05:00
|
|
|
}
|
2023-11-21 20:07:32 +01:00
|
|
|
GenericArg::Type(self.lower_ty(ty, itctx))
|
2019-11-18 14:30:01 -05:00
|
|
|
}
|
2019-12-24 17:38:22 -05:00
|
|
|
ast::GenericArg::Const(ct) => GenericArg::Const(ConstArg {
|
2023-11-21 20:07:32 +01:00
|
|
|
value: self.lower_anon_const(ct),
|
2021-08-21 00:29:08 +03:00
|
|
|
span: self.lower_span(ct.value.span),
|
2023-10-25 15:28:23 +00:00
|
|
|
is_desugared_from_effects: false,
|
2019-12-24 17:38:22 -05:00
|
|
|
}),
|
2018-02-08 08:58:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-20 15:52:56 -03:00
|
|
|
#[instrument(level = "debug", skip(self))]
|
2024-02-07 19:27:44 +00:00
|
|
|
fn lower_ty(&mut self, t: &Ty, itctx: ImplTraitContext) -> &'hir hir::Ty<'hir> {
|
2022-07-26 15:50:25 -03:00
|
|
|
self.arena.alloc(self.lower_ty_direct(t, itctx))
|
2018-06-20 19:05:27 +02:00
|
|
|
}
|
|
|
|
|
2019-06-08 21:35:02 -07:00
|
|
|
fn lower_path_ty(
|
|
|
|
&mut self,
|
|
|
|
t: &Ty,
|
2022-09-08 10:52:51 +10:00
|
|
|
qself: &Option<ptr::P<QSelf>>,
|
2019-06-08 21:35:02 -07:00
|
|
|
path: &Path,
|
|
|
|
param_mode: ParamMode,
|
2024-02-07 19:27:44 +00:00
|
|
|
itctx: ImplTraitContext,
|
2019-11-30 17:46:46 +01:00
|
|
|
) -> hir::Ty<'hir> {
|
2022-06-25 19:16:56 +02:00
|
|
|
// Check whether we should interpret this as a bare trait object.
|
2022-11-16 20:34:16 +00:00
|
|
|
// This check mirrors the one in late resolution. We only introduce this special case in
|
2022-08-18 10:13:37 +08:00
|
|
|
// the rare occurrence we need to lower `Fresh` anonymous lifetimes.
|
2022-06-25 19:16:56 +02:00
|
|
|
// The other cases when a qpath should be opportunistically made a trait object are handled
|
|
|
|
// by `ty_path`.
|
|
|
|
if qself.is_none()
|
|
|
|
&& let Some(partial_res) = self.resolver.get_partial_res(t.id)
|
2022-10-10 19:21:35 +04:00
|
|
|
&& let Some(Res::Def(DefKind::Trait | DefKind::TraitAlias, _)) = partial_res.full_res()
|
2022-06-25 19:16:56 +02:00
|
|
|
{
|
|
|
|
let (bounds, lifetime_bound) = self.with_dyn_type_scope(true, |this| {
|
|
|
|
let bound = this.lower_poly_trait_ref(
|
2022-12-18 20:28:59 +01:00
|
|
|
&PolyTraitRef {
|
2022-11-22 09:17:20 +11:00
|
|
|
bound_generic_params: ThinVec::new(),
|
2022-12-18 20:28:59 +01:00
|
|
|
trait_ref: TraitRef { path: path.clone(), ref_id: t.id },
|
|
|
|
span: t.span,
|
|
|
|
},
|
2022-06-25 19:16:56 +02:00
|
|
|
itctx,
|
2024-01-26 17:00:28 +00:00
|
|
|
TraitBoundModifiers::NONE,
|
2022-06-25 19:16:56 +02:00
|
|
|
);
|
|
|
|
let bounds = this.arena.alloc_from_iter([bound]);
|
|
|
|
let lifetime_bound = this.elided_dyn_bound(t.span);
|
|
|
|
(bounds, lifetime_bound)
|
|
|
|
});
|
2023-11-21 20:07:32 +01:00
|
|
|
let kind = hir::TyKind::TraitObject(bounds, lifetime_bound, TraitObjectSyntax::None);
|
2022-06-25 19:16:56 +02:00
|
|
|
return hir::Ty { kind, span: self.lower_span(t.span), hir_id: self.next_id() };
|
|
|
|
}
|
|
|
|
|
2019-06-08 21:35:02 -07:00
|
|
|
let id = self.lower_node_id(t.id);
|
2023-07-25 05:58:53 +00:00
|
|
|
let qpath = self.lower_qpath(t.id, qself, path, param_mode, itctx, None);
|
2021-07-10 10:00:54 +02:00
|
|
|
self.ty_path(id, t.span, qpath)
|
2019-06-08 21:35:02 -07:00
|
|
|
}
|
|
|
|
|
2019-11-30 17:46:46 +01:00
|
|
|
fn ty(&mut self, span: Span, kind: hir::TyKind<'hir>) -> hir::Ty<'hir> {
|
2021-08-21 00:29:08 +03:00
|
|
|
hir::Ty { hir_id: self.next_id(), kind, span: self.lower_span(span) }
|
2019-12-01 10:25:45 +01:00
|
|
|
}
|
|
|
|
|
2019-12-01 11:22:58 +01:00
|
|
|
fn ty_tup(&mut self, span: Span, tys: &'hir [hir::Ty<'hir>]) -> hir::Ty<'hir> {
|
2019-12-01 10:25:45 +01:00
|
|
|
self.ty(span, hir::TyKind::Tup(tys))
|
|
|
|
}
|
|
|
|
|
2024-02-07 19:27:44 +00:00
|
|
|
fn lower_ty_direct(&mut self, t: &Ty, itctx: ImplTraitContext) -> hir::Ty<'hir> {
|
2022-11-22 15:37:54 +00:00
|
|
|
let kind = match &t.kind {
|
2018-07-11 22:41:03 +08:00
|
|
|
TyKind::Infer => hir::TyKind::Infer,
|
2024-02-14 14:50:49 +11:00
|
|
|
TyKind::Err(guar) => hir::TyKind::Err(*guar),
|
2024-01-04 21:53:06 +08:00
|
|
|
// Lower the anonymous structs or unions in a nested lowering context.
|
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// struct Foo {
|
|
|
|
// _: union {
|
|
|
|
// // ^__________________ <-- within the nested lowering context,
|
|
|
|
// /* fields */ // | we lower all fields defined into an
|
|
|
|
// } // | owner node of struct or union item
|
|
|
|
// // ^_____________________|
|
|
|
|
// }
|
|
|
|
// ```
|
2024-01-04 21:45:06 +08:00
|
|
|
TyKind::AnonStruct(node_id, fields) | TyKind::AnonUnion(node_id, fields) => {
|
|
|
|
// Here its `def_id` is created in `build_reduced_graph`.
|
|
|
|
let def_id = self.local_def_id(*node_id);
|
2024-01-04 21:53:06 +08:00
|
|
|
debug!(?def_id);
|
|
|
|
let owner_id = hir::OwnerId { def_id };
|
2024-01-04 21:45:06 +08:00
|
|
|
self.with_hir_id_owner(*node_id, |this| {
|
2024-01-04 21:53:06 +08:00
|
|
|
let fields = this.arena.alloc_from_iter(
|
|
|
|
fields.iter().enumerate().map(|f| this.lower_field_def(f)),
|
|
|
|
);
|
|
|
|
let span = t.span;
|
2024-01-04 21:45:06 +08:00
|
|
|
let variant_data = hir::VariantData::Struct { fields, recovered: false };
|
2024-01-04 21:53:06 +08:00
|
|
|
// FIXME: capture the generics from the outer adt.
|
|
|
|
let generics = hir::Generics::empty();
|
2024-01-04 21:45:06 +08:00
|
|
|
let kind = match t.kind {
|
|
|
|
TyKind::AnonStruct(..) => hir::ItemKind::Struct(variant_data, generics),
|
|
|
|
TyKind::AnonUnion(..) => hir::ItemKind::Union(variant_data, generics),
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
2024-01-04 21:53:06 +08:00
|
|
|
hir::OwnerNode::Item(this.arena.alloc(hir::Item {
|
|
|
|
ident: Ident::new(kw::Empty, span),
|
|
|
|
owner_id,
|
2024-01-04 21:45:06 +08:00
|
|
|
kind,
|
2024-01-04 21:53:06 +08:00
|
|
|
span: this.lower_span(span),
|
|
|
|
vis_span: this.lower_span(span.shrink_to_lo()),
|
|
|
|
}))
|
|
|
|
});
|
|
|
|
hir::TyKind::AnonAdt(hir::ItemId { owner_id })
|
2023-12-18 22:21:37 +11:00
|
|
|
}
|
2022-11-22 15:37:54 +00:00
|
|
|
TyKind::Slice(ty) => hir::TyKind::Slice(self.lower_ty(ty, itctx)),
|
|
|
|
TyKind::Ptr(mt) => hir::TyKind::Ptr(self.lower_mt(mt, itctx)),
|
2022-12-28 18:06:11 +01:00
|
|
|
TyKind::Ref(region, mt) => {
|
2022-04-07 20:54:13 +02:00
|
|
|
let region = region.unwrap_or_else(|| {
|
2022-05-21 00:18:50 +02:00
|
|
|
let id = if let Some(LifetimeRes::ElidedAnchor { start, end }) =
|
|
|
|
self.resolver.get_lifetime_res(t.id)
|
|
|
|
{
|
|
|
|
debug_assert_eq!(start.plus(1), end);
|
|
|
|
start
|
|
|
|
} else {
|
2021-07-18 20:09:20 +02:00
|
|
|
self.next_node_id()
|
2022-04-07 20:54:13 +02:00
|
|
|
};
|
2022-11-05 22:41:07 +00:00
|
|
|
let span = self.tcx.sess.source_map().start_point(t.span).shrink_to_hi();
|
2022-05-21 00:18:50 +02:00
|
|
|
Lifetime { ident: Ident::new(kw::UnderscoreLifetime, span), id }
|
2022-04-07 20:54:13 +02:00
|
|
|
});
|
2022-07-26 15:50:25 -03:00
|
|
|
let lifetime = self.lower_lifetime(®ion);
|
2022-12-28 18:06:11 +01:00
|
|
|
hir::TyKind::Ref(lifetime, self.lower_mt(mt, itctx))
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
2022-11-22 15:37:54 +00:00
|
|
|
TyKind::BareFn(f) => {
|
2022-09-14 17:14:36 -03:00
|
|
|
let generic_params = self.lower_lifetime_binder(t.id, &f.generic_params);
|
|
|
|
hir::TyKind::BareFn(self.arena.alloc(hir::BareFnTy {
|
|
|
|
generic_params,
|
|
|
|
unsafety: self.lower_unsafety(f.unsafety),
|
|
|
|
abi: self.lower_extern(f.ext),
|
2023-11-30 16:39:56 -08:00
|
|
|
decl: self.lower_fn_decl(&f.decl, t.id, t.span, FnDeclKind::Pointer, None),
|
2022-09-14 17:14:36 -03:00
|
|
|
param_names: self.lower_fn_params_to_names(&f.decl),
|
|
|
|
}))
|
2022-05-11 22:49:39 +02:00
|
|
|
}
|
2018-07-11 22:41:03 +08:00
|
|
|
TyKind::Never => hir::TyKind::Never,
|
2022-11-22 15:37:54 +00:00
|
|
|
TyKind::Tup(tys) => hir::TyKind::Tup(
|
2022-07-26 15:50:25 -03:00
|
|
|
self.arena.alloc_from_iter(tys.iter().map(|ty| self.lower_ty_direct(ty, itctx))),
|
|
|
|
),
|
2022-11-22 15:37:54 +00:00
|
|
|
TyKind::Paren(ty) => {
|
2022-07-26 15:50:25 -03:00
|
|
|
return self.lower_ty_direct(ty, itctx);
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
2022-11-22 15:37:54 +00:00
|
|
|
TyKind::Path(qself, path) => {
|
2022-07-26 15:50:25 -03:00
|
|
|
return self.lower_path_ty(t, qself, path, ParamMode::Explicit, itctx);
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
2019-04-03 09:07:45 +02:00
|
|
|
TyKind::ImplicitSelf => {
|
2022-09-11 15:11:58 +02:00
|
|
|
let hir_id = self.next_id();
|
2019-04-20 19:36:05 +03:00
|
|
|
let res = self.expect_full_res(t.id);
|
|
|
|
let res = self.lower_res(res);
|
2019-04-03 09:07:45 +02:00
|
|
|
hir::TyKind::Path(hir::QPath::Resolved(
|
|
|
|
None,
|
2019-12-01 11:22:58 +01:00
|
|
|
self.arena.alloc(hir::Path {
|
2019-04-20 19:36:05 +03:00
|
|
|
res,
|
2022-09-01 08:44:20 +10:00
|
|
|
segments: arena_vec![self; hir::PathSegment::new(
|
2022-08-30 15:10:28 +10:00
|
|
|
Ident::with_dummy_span(kw::SelfUpper),
|
2022-08-30 16:54:42 +10:00
|
|
|
hir_id,
|
2022-08-30 15:10:28 +10:00
|
|
|
res
|
2019-12-01 11:22:58 +01:00
|
|
|
)],
|
2021-08-21 00:29:08 +03:00
|
|
|
span: self.lower_span(t.span),
|
2019-04-03 09:07:45 +02:00
|
|
|
}),
|
|
|
|
))
|
2019-12-24 17:38:22 -05:00
|
|
|
}
|
2022-11-22 15:37:54 +00:00
|
|
|
TyKind::Array(ty, length) => {
|
2022-07-26 15:50:25 -03:00
|
|
|
hir::TyKind::Array(self.lower_ty(ty, itctx), self.lower_array_length(length))
|
|
|
|
}
|
2022-11-22 15:37:54 +00:00
|
|
|
TyKind::Typeof(expr) => hir::TyKind::Typeof(self.lower_anon_const(expr)),
|
|
|
|
TyKind::TraitObject(bounds, kind) => {
|
2017-03-14 15:50:40 +01:00
|
|
|
let mut lifetime_bound = None;
|
2019-03-21 17:55:09 +00:00
|
|
|
let (bounds, lifetime_bound) = self.with_dyn_type_scope(true, |this| {
|
2019-12-01 11:22:58 +01:00
|
|
|
let bounds =
|
2022-11-22 15:37:54 +00:00
|
|
|
this.arena.alloc_from_iter(bounds.iter().filter_map(|bound| match bound {
|
2024-01-03 09:24:42 +01:00
|
|
|
// We can safely ignore constness here since AST validation
|
|
|
|
// takes care of rejecting invalid modifier combinations and
|
|
|
|
// const trait bounds in trait object types.
|
|
|
|
GenericBound::Trait(ty, modifiers) => match modifiers.polarity {
|
|
|
|
BoundPolarity::Positive | BoundPolarity::Negative(_) => {
|
|
|
|
Some(this.lower_poly_trait_ref(
|
|
|
|
ty,
|
|
|
|
itctx,
|
|
|
|
// Still, don't pass along the constness here; we don't want to
|
|
|
|
// synthesize any host effect args, it'd only cause problems.
|
2024-01-26 17:00:28 +00:00
|
|
|
TraitBoundModifiers {
|
|
|
|
constness: BoundConstness::Never,
|
|
|
|
..*modifiers
|
|
|
|
},
|
2024-01-03 09:24:42 +01:00
|
|
|
))
|
|
|
|
}
|
|
|
|
BoundPolarity::Maybe(_) => None,
|
|
|
|
},
|
2022-11-22 15:37:54 +00:00
|
|
|
GenericBound::Outlives(lifetime) => {
|
|
|
|
if lifetime_bound.is_none() {
|
|
|
|
lifetime_bound = Some(this.lower_lifetime(lifetime));
|
2019-12-01 11:22:58 +01:00
|
|
|
}
|
2022-11-22 15:37:54 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
}));
|
2019-03-21 17:55:09 +00:00
|
|
|
let lifetime_bound =
|
|
|
|
lifetime_bound.unwrap_or_else(|| this.elided_dyn_bound(t.span));
|
|
|
|
(bounds, lifetime_bound)
|
|
|
|
});
|
2022-11-22 15:37:54 +00:00
|
|
|
hir::TyKind::TraitObject(bounds, lifetime_bound, *kind)
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
2024-04-03 19:58:50 -04:00
|
|
|
TyKind::ImplTrait(def_node_id, bounds, precise_capturing) => {
|
2017-12-15 12:27:20 -08:00
|
|
|
let span = t.span;
|
2017-11-10 12:23:59 -05:00
|
|
|
match itctx {
|
2024-03-06 18:44:55 +00:00
|
|
|
ImplTraitContext::OpaqueTy { origin, fn_kind } => self.lower_opaque_impl_trait(
|
|
|
|
span,
|
|
|
|
origin,
|
|
|
|
*def_node_id,
|
|
|
|
bounds,
|
|
|
|
fn_kind,
|
|
|
|
itctx,
|
2024-04-04 12:54:56 -04:00
|
|
|
precise_capturing.as_deref().map(|(args, _)| args.as_slice()),
|
2024-03-06 18:44:55 +00:00
|
|
|
),
|
2022-06-03 20:17:12 +02:00
|
|
|
ImplTraitContext::Universal => {
|
2024-04-04 12:54:56 -04:00
|
|
|
if let Some(&(_, span)) = precise_capturing.as_deref() {
|
|
|
|
self.tcx.dcx().emit_err(errors::NoPreciseCapturesOnApit { span });
|
|
|
|
};
|
2022-12-01 10:33:28 +00:00
|
|
|
let span = t.span;
|
2023-05-25 01:41:13 +00:00
|
|
|
|
|
|
|
// HACK: pprust breaks strings with newlines when the type
|
|
|
|
// gets too long. We don't want these to show up in compiler
|
|
|
|
// output or built artifacts, so replace them here...
|
|
|
|
// Perhaps we should instead format APITs more robustly.
|
|
|
|
let ident = Ident::from_str_and_span(
|
|
|
|
&pprust::ty_to_string(t).replace('\n', " "),
|
|
|
|
span,
|
|
|
|
);
|
|
|
|
|
2022-04-08 23:29:07 +02:00
|
|
|
self.create_def(
|
|
|
|
self.current_hir_id_owner.def_id,
|
|
|
|
*def_node_id,
|
2023-12-03 12:29:59 +03:00
|
|
|
ident.name,
|
2023-11-21 23:40:23 +03:00
|
|
|
DefKind::TyParam,
|
2022-04-08 23:29:07 +02:00
|
|
|
span,
|
|
|
|
);
|
2023-02-28 06:26:48 +00:00
|
|
|
let (param, bounds, path) = self.lower_universal_param_and_bounds(
|
|
|
|
*def_node_id,
|
|
|
|
span,
|
|
|
|
ident,
|
|
|
|
bounds,
|
|
|
|
);
|
2022-05-31 11:31:57 -03:00
|
|
|
self.impl_trait_defs.push(param);
|
2022-06-07 18:17:41 -03:00
|
|
|
if let Some(bounds) = bounds {
|
|
|
|
self.impl_trait_bounds.push(bounds);
|
2022-02-07 22:58:30 +01:00
|
|
|
}
|
2022-06-07 18:17:41 -03:00
|
|
|
path
|
2018-03-21 04:24:27 -04:00
|
|
|
}
|
2022-12-06 17:53:50 -08:00
|
|
|
ImplTraitContext::FeatureGated(position, feature) => {
|
2023-02-22 21:19:42 +00:00
|
|
|
let guar = self
|
|
|
|
.tcx
|
2022-09-06 17:27:47 +00:00
|
|
|
.sess
|
|
|
|
.create_feature_err(
|
|
|
|
MisplacedImplTrait {
|
|
|
|
span: t.span,
|
2024-02-23 15:34:39 +11:00
|
|
|
position: DiagArgFromDisplay(&position),
|
2022-09-06 17:27:47 +00:00
|
|
|
},
|
2024-02-07 19:27:44 +00:00
|
|
|
feature,
|
2022-09-06 17:27:47 +00:00
|
|
|
)
|
|
|
|
.emit();
|
2023-02-22 21:19:42 +00:00
|
|
|
hir::TyKind::Err(guar)
|
2022-09-06 17:16:08 +00:00
|
|
|
}
|
2022-01-11 19:00:34 -08:00
|
|
|
ImplTraitContext::Disallowed(position) => {
|
2023-12-18 22:21:37 +11:00
|
|
|
let guar = self.dcx().emit_err(MisplacedImplTrait {
|
2022-08-17 16:58:57 +02:00
|
|
|
span: t.span,
|
2024-02-23 15:34:39 +11:00
|
|
|
position: DiagArgFromDisplay(&position),
|
2022-08-17 16:58:57 +02:00
|
|
|
});
|
2023-02-22 21:19:42 +00:00
|
|
|
hir::TyKind::Err(guar)
|
2017-11-10 12:23:59 -05:00
|
|
|
}
|
|
|
|
}
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
2023-01-31 11:54:06 +00:00
|
|
|
TyKind::Pat(ty, pat) => hir::TyKind::Pat(self.lower_ty(ty, itctx), self.lower_pat(pat)),
|
2023-01-30 09:50:16 +00:00
|
|
|
TyKind::MacCall(_) => {
|
|
|
|
span_bug!(t.span, "`TyKind::MacCall` should have been expanded by now")
|
|
|
|
}
|
2019-12-02 03:16:12 +01:00
|
|
|
TyKind::CVarArgs => {
|
2023-12-18 22:21:37 +11:00
|
|
|
let guar = self.dcx().span_delayed_bug(
|
2019-12-02 03:16:12 +01:00
|
|
|
t.span,
|
|
|
|
"`TyKind::CVarArgs` should have been handled elsewhere",
|
|
|
|
);
|
2023-02-22 21:19:42 +00:00
|
|
|
hir::TyKind::Err(guar)
|
2019-12-02 03:16:12 +01:00
|
|
|
}
|
2024-02-14 14:50:49 +11:00
|
|
|
TyKind::Dummy => panic!("`TyKind::Dummy` should never be lowered"),
|
2017-03-14 15:50:40 +01:00
|
|
|
};
|
|
|
|
|
2021-08-21 00:29:08 +03:00
|
|
|
hir::Ty { kind, span: self.lower_span(t.span), hir_id: self.lower_node_id(t.id) }
|
2015-12-07 17:17:41 +03:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2022-08-03 19:34:00 -03:00
|
|
|
/// Lowers a `ReturnPositionOpaqueTy` (`-> impl Trait`) or a `TypeAliasesOpaqueTy` (`type F =
|
|
|
|
/// impl Trait`): this creates the associated Opaque Type (TAIT) definition and then returns a
|
|
|
|
/// HIR type that references the TAIT.
|
|
|
|
///
|
|
|
|
/// Given a function definition like:
|
|
|
|
///
|
|
|
|
/// ```rust
|
2023-05-07 00:12:29 +03:00
|
|
|
/// use std::fmt::Debug;
|
|
|
|
///
|
2022-08-03 19:34:00 -03:00
|
|
|
/// fn test<'a, T: Debug>(x: &'a T) -> impl Debug + 'a {
|
|
|
|
/// x
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// we will create a TAIT definition in the HIR like
|
|
|
|
///
|
2023-05-07 00:12:29 +03:00
|
|
|
/// ```rust,ignore (pseudo-Rust)
|
2022-08-03 19:34:00 -03:00
|
|
|
/// type TestReturn<'a, T, 'x> = impl Debug + 'x
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// and return a type like `TestReturn<'static, T, 'a>`, so that the function looks like:
|
|
|
|
///
|
2023-05-07 00:12:29 +03:00
|
|
|
/// ```rust,ignore (pseudo-Rust)
|
2022-08-03 19:34:00 -03:00
|
|
|
/// fn test<'a, T: Debug>(x: &'a T) -> TestReturn<'static, T, 'a>
|
|
|
|
/// ```
|
|
|
|
///
|
2022-08-04 12:48:20 -03:00
|
|
|
/// Note the subtlety around type parameters! The new TAIT, `TestReturn`, inherits all the
|
2022-08-03 19:34:00 -03:00
|
|
|
/// type parameters from the function `test` (this is implemented in the query layer, they aren't
|
|
|
|
/// added explicitly in the HIR). But this includes all the lifetimes, and we only want to
|
|
|
|
/// capture the lifetimes that are referenced in the bounds. Therefore, we add *extra* lifetime parameters
|
|
|
|
/// for the lifetimes that get captured (`'x`, in our example above) and reference those.
|
2022-09-06 17:24:36 -03:00
|
|
|
#[instrument(level = "debug", skip(self), ret)]
|
2019-08-01 00:41:54 +01:00
|
|
|
fn lower_opaque_impl_trait(
|
2018-06-06 15:50:59 -07:00
|
|
|
&mut self,
|
|
|
|
span: Span,
|
2019-12-28 15:39:52 +00:00
|
|
|
origin: hir::OpaqueTyOrigin,
|
2019-08-02 00:08:05 +01:00
|
|
|
opaque_ty_node_id: NodeId,
|
2022-07-20 16:30:37 -03:00
|
|
|
bounds: &GenericBounds,
|
2023-08-04 23:54:14 +00:00
|
|
|
fn_kind: Option<FnDeclKind>,
|
2024-02-07 19:27:44 +00:00
|
|
|
itctx: ImplTraitContext,
|
2024-04-04 10:48:47 -04:00
|
|
|
precise_capturing_args: Option<&[PreciseCapturingArg]>,
|
2019-11-30 17:46:46 +01:00
|
|
|
) -> hir::TyKind<'hir> {
|
2018-06-06 15:50:59 -07:00
|
|
|
// Make sure we know that some funky desugaring has been going on here.
|
|
|
|
// This is a first: there is code in other places like for loop
|
|
|
|
// desugaring that explicitly states that we don't want to track that.
|
2019-02-28 22:43:53 +00:00
|
|
|
// Not tracking it makes lints in rustc and clippy very fragile, as
|
2018-06-06 15:50:59 -07:00
|
|
|
// frequently opened issues show.
|
2019-12-24 17:38:22 -05:00
|
|
|
let opaque_ty_span = self.mark_span_with_reason(DesugaringKind::OpaqueTy, span, None);
|
2018-06-06 15:50:59 -07:00
|
|
|
|
2024-04-03 21:47:02 -04:00
|
|
|
let captured_lifetimes_to_duplicate =
|
|
|
|
if let Some(precise_capturing) = precise_capturing_args {
|
|
|
|
// We'll actually validate these later on; all we need is the list of
|
|
|
|
// lifetimes to duplicate during this portion of lowering.
|
|
|
|
precise_capturing
|
|
|
|
.iter()
|
|
|
|
.filter_map(|arg| match arg {
|
2024-04-04 10:48:47 -04:00
|
|
|
PreciseCapturingArg::Lifetime(lt) => Some(*lt),
|
|
|
|
PreciseCapturingArg::Arg(..) => None,
|
2024-04-03 21:47:02 -04:00
|
|
|
})
|
2024-04-04 10:48:47 -04:00
|
|
|
// Add in all the lifetimes mentioned in the bounds. We will error
|
|
|
|
// them out later, but capturing them here is important to make sure
|
|
|
|
// they actually get resolved in resolve_bound_vars.
|
|
|
|
.chain(lifetime_collector::lifetimes_in_bounds(self.resolver, bounds))
|
2024-04-03 21:47:02 -04:00
|
|
|
.collect()
|
|
|
|
} else {
|
|
|
|
match origin {
|
|
|
|
hir::OpaqueTyOrigin::TyAlias { .. } => {
|
|
|
|
// type alias impl trait and associated type position impl trait were
|
|
|
|
// decided to capture all in-scope lifetimes, which we collect for
|
|
|
|
// all opaques during resolution.
|
2024-04-03 20:54:23 -04:00
|
|
|
self.resolver
|
|
|
|
.take_extra_lifetime_params(opaque_ty_node_id)
|
|
|
|
.into_iter()
|
|
|
|
.map(|(ident, id, _)| Lifetime { id, ident })
|
|
|
|
.collect()
|
2024-04-03 21:47:02 -04:00
|
|
|
}
|
|
|
|
hir::OpaqueTyOrigin::FnReturn(..) => {
|
|
|
|
if matches!(
|
|
|
|
fn_kind.expect("expected RPITs to be lowered with a FnKind"),
|
|
|
|
FnDeclKind::Impl | FnDeclKind::Trait
|
|
|
|
) || self.tcx.features().lifetime_capture_rules_2024
|
|
|
|
|| span.at_least_rust_2024()
|
|
|
|
{
|
|
|
|
// return-position impl trait in trait was decided to capture all
|
|
|
|
// in-scope lifetimes, which we collect for all opaques during resolution.
|
|
|
|
self.resolver
|
|
|
|
.take_extra_lifetime_params(opaque_ty_node_id)
|
|
|
|
.into_iter()
|
|
|
|
.map(|(ident, id, _)| Lifetime { id, ident })
|
|
|
|
.collect()
|
|
|
|
} else {
|
|
|
|
// in fn return position, like the `fn test<'a>() -> impl Debug + 'a`
|
|
|
|
// example, we only need to duplicate lifetimes that appear in the
|
|
|
|
// bounds, since those are the only ones that are captured by the opaque.
|
|
|
|
lifetime_collector::lifetimes_in_bounds(self.resolver, bounds)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
hir::OpaqueTyOrigin::AsyncFn(..) => {
|
|
|
|
unreachable!("should be using `lower_async_fn_ret_ty`")
|
2024-04-03 20:54:23 -04:00
|
|
|
}
|
|
|
|
}
|
2024-04-03 21:47:02 -04:00
|
|
|
};
|
2023-08-04 22:16:29 +00:00
|
|
|
debug!(?captured_lifetimes_to_duplicate);
|
2022-08-02 20:01:40 -03:00
|
|
|
|
2023-08-04 22:16:29 +00:00
|
|
|
self.lower_opaque_inner(
|
|
|
|
opaque_ty_node_id,
|
|
|
|
origin,
|
2023-08-04 23:54:14 +00:00
|
|
|
matches!(fn_kind, Some(FnDeclKind::Trait)),
|
2023-08-04 22:16:29 +00:00
|
|
|
captured_lifetimes_to_duplicate,
|
|
|
|
span,
|
|
|
|
opaque_ty_span,
|
2024-04-04 10:48:47 -04:00
|
|
|
precise_capturing_args,
|
2023-08-04 22:16:29 +00:00
|
|
|
|this| this.lower_param_bounds(bounds, itctx),
|
2022-09-20 14:11:23 +09:00
|
|
|
)
|
2022-08-31 04:03:24 +00:00
|
|
|
}
|
|
|
|
|
2023-08-04 22:16:29 +00:00
|
|
|
fn lower_opaque_inner(
|
2019-03-13 17:42:23 -07:00
|
|
|
&mut self,
|
2023-08-04 22:16:29 +00:00
|
|
|
opaque_ty_node_id: NodeId,
|
|
|
|
origin: hir::OpaqueTyOrigin,
|
|
|
|
in_trait: bool,
|
2024-04-04 10:48:47 -04:00
|
|
|
captured_lifetimes_to_duplicate: FxIndexSet<Lifetime>,
|
2019-03-13 17:42:23 -07:00
|
|
|
span: Span,
|
2019-08-02 00:08:05 +01:00
|
|
|
opaque_ty_span: Span,
|
2024-04-04 10:48:47 -04:00
|
|
|
precise_capturing_args: Option<&[PreciseCapturingArg]>,
|
2023-08-04 22:16:29 +00:00
|
|
|
lower_item_bounds: impl FnOnce(&mut Self) -> &'hir [hir::GenericBound<'hir>],
|
|
|
|
) -> hir::TyKind<'hir> {
|
|
|
|
let opaque_ty_def_id = self.create_def(
|
|
|
|
self.current_hir_id_owner.def_id,
|
|
|
|
opaque_ty_node_id,
|
2023-12-03 12:29:59 +03:00
|
|
|
kw::Empty,
|
2023-11-21 23:40:23 +03:00
|
|
|
DefKind::OpaqueTy,
|
2023-08-04 22:16:29 +00:00
|
|
|
opaque_ty_span,
|
|
|
|
);
|
|
|
|
debug!(?opaque_ty_def_id);
|
2022-08-02 20:14:24 -03:00
|
|
|
|
2023-08-04 22:16:29 +00:00
|
|
|
// Map from captured (old) lifetime to synthetic (new) lifetime.
|
|
|
|
// Used to resolve lifetimes in the bounds of the opaque.
|
2023-12-18 21:02:21 +01:00
|
|
|
let mut captured_to_synthesized_mapping = LocalDefIdMap::default();
|
2023-08-04 22:16:29 +00:00
|
|
|
// List of (early-bound) synthetic lifetimes that are owned by the opaque.
|
|
|
|
// This is used to create the `hir::Generics` owned by the opaque.
|
|
|
|
let mut synthesized_lifetime_definitions = vec![];
|
|
|
|
// Pairs of lifetime arg (that resolves to the captured lifetime)
|
|
|
|
// and the def-id of the (early-bound) synthetic lifetime definition.
|
|
|
|
// This is used both to create generics for the `TyKind::OpaqueDef` that
|
|
|
|
// we return, and also as a captured lifetime mapping for RPITITs.
|
|
|
|
let mut synthesized_lifetime_args = vec![];
|
|
|
|
|
|
|
|
for lifetime in captured_lifetimes_to_duplicate {
|
2022-07-26 14:49:24 -03:00
|
|
|
let res = self.resolver.get_lifetime_res(lifetime.id).unwrap_or(LifetimeRes::Error);
|
2024-02-25 21:46:11 +09:00
|
|
|
let (old_def_id, missing_kind) = match res {
|
|
|
|
LifetimeRes::Param { param: old_def_id, binder: _ } => (old_def_id, None),
|
2022-07-26 14:49:24 -03:00
|
|
|
|
2024-02-25 21:46:11 +09:00
|
|
|
LifetimeRes::Fresh { param, kind, .. } => {
|
2022-08-02 20:14:24 -03:00
|
|
|
debug_assert_eq!(lifetime.ident.name, kw::UnderscoreLifetime);
|
2023-08-04 22:16:29 +00:00
|
|
|
if let Some(old_def_id) = self.orig_opt_local_def_id(param) {
|
2024-02-25 21:46:11 +09:00
|
|
|
(old_def_id, Some(kind))
|
2023-08-04 22:16:29 +00:00
|
|
|
} else {
|
2024-02-22 14:41:32 +11:00
|
|
|
self.dcx()
|
|
|
|
.span_delayed_bug(lifetime.ident.span, "no def-id for fresh lifetime");
|
|
|
|
continue;
|
2022-08-02 20:14:24 -03:00
|
|
|
}
|
2022-07-26 14:49:24 -03:00
|
|
|
}
|
|
|
|
|
2023-08-04 22:16:29 +00:00
|
|
|
// Opaques do not capture `'static`
|
|
|
|
LifetimeRes::Static | LifetimeRes::Error => {
|
|
|
|
continue;
|
|
|
|
}
|
2022-08-02 20:14:24 -03:00
|
|
|
|
2022-08-04 12:07:03 -03:00
|
|
|
res => {
|
|
|
|
let bug_msg = format!(
|
|
|
|
"Unexpected lifetime resolution {:?} for {:?} at {:?}",
|
|
|
|
res, lifetime.ident, lifetime.ident.span
|
|
|
|
);
|
|
|
|
span_bug!(lifetime.ident.span, "{}", bug_msg);
|
|
|
|
}
|
2023-08-04 22:16:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if captured_to_synthesized_mapping.get(&old_def_id).is_none() {
|
|
|
|
// Create a new lifetime parameter local to the opaque.
|
|
|
|
let duplicated_lifetime_node_id = self.next_node_id();
|
|
|
|
let duplicated_lifetime_def_id = self.create_def(
|
|
|
|
opaque_ty_def_id,
|
|
|
|
duplicated_lifetime_node_id,
|
2023-12-03 12:29:59 +03:00
|
|
|
lifetime.ident.name,
|
2023-11-21 23:40:23 +03:00
|
|
|
DefKind::LifetimeParam,
|
2023-12-09 00:02:34 +00:00
|
|
|
self.lower_span(lifetime.ident.span),
|
2023-08-04 22:16:29 +00:00
|
|
|
);
|
|
|
|
captured_to_synthesized_mapping.insert(old_def_id, duplicated_lifetime_def_id);
|
|
|
|
// FIXME: Instead of doing this, we could move this whole loop
|
|
|
|
// into the `with_hir_id_owner`, then just directly construct
|
|
|
|
// the `hir::GenericParam` here.
|
|
|
|
synthesized_lifetime_definitions.push((
|
|
|
|
duplicated_lifetime_node_id,
|
|
|
|
duplicated_lifetime_def_id,
|
2023-12-09 00:02:34 +00:00
|
|
|
self.lower_ident(lifetime.ident),
|
2024-02-25 21:46:11 +09:00
|
|
|
missing_kind,
|
2023-08-04 22:16:29 +00:00
|
|
|
));
|
|
|
|
|
2023-09-25 15:46:38 +02:00
|
|
|
// Now make an arg that we can use for the generic params of the opaque tykind.
|
2023-08-04 22:16:29 +00:00
|
|
|
let id = self.next_node_id();
|
|
|
|
let lifetime_arg = self.new_named_lifetime_with_res(id, lifetime.ident, res);
|
|
|
|
let duplicated_lifetime_def_id = self.local_def_id(duplicated_lifetime_node_id);
|
|
|
|
synthesized_lifetime_args.push((lifetime_arg, duplicated_lifetime_def_id))
|
2022-07-26 14:49:24 -03:00
|
|
|
}
|
|
|
|
}
|
2022-08-02 20:14:24 -03:00
|
|
|
|
2023-08-04 22:16:29 +00:00
|
|
|
self.with_hir_id_owner(opaque_ty_node_id, |this| {
|
|
|
|
// Install the remapping from old to new (if any). This makes sure that
|
|
|
|
// any lifetimes that would have resolved to the def-id of captured
|
|
|
|
// lifetimes are remapped to the new *synthetic* lifetimes of the opaque.
|
2024-04-04 10:48:47 -04:00
|
|
|
let (bounds, precise_capturing_args) =
|
|
|
|
this.with_remapping(captured_to_synthesized_mapping, |this| {
|
|
|
|
(
|
|
|
|
lower_item_bounds(this),
|
|
|
|
precise_capturing_args.map(|precise_capturing| {
|
|
|
|
this.lower_precise_capturing_args(precise_capturing)
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
});
|
2023-08-04 22:16:29 +00:00
|
|
|
|
2024-02-25 21:46:11 +09:00
|
|
|
let generic_params =
|
|
|
|
this.arena.alloc_from_iter(synthesized_lifetime_definitions.iter().map(
|
|
|
|
|&(new_node_id, new_def_id, ident, missing_kind)| {
|
|
|
|
let hir_id = this.lower_node_id(new_node_id);
|
|
|
|
let (name, kind) = if ident.name == kw::UnderscoreLifetime {
|
|
|
|
(
|
|
|
|
hir::ParamName::Fresh,
|
|
|
|
hir::LifetimeParamKind::Elided(
|
|
|
|
missing_kind.unwrap_or(MissingLifetimeKind::Underscore),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
(hir::ParamName::Plain(ident), hir::LifetimeParamKind::Explicit)
|
|
|
|
};
|
2023-08-04 22:16:29 +00:00
|
|
|
|
2024-02-25 21:46:11 +09:00
|
|
|
hir::GenericParam {
|
|
|
|
hir_id,
|
|
|
|
def_id: new_def_id,
|
|
|
|
name,
|
|
|
|
span: ident.span,
|
|
|
|
pure_wrt_drop: false,
|
|
|
|
kind: hir::GenericParamKind::Lifetime { kind },
|
|
|
|
colon_span: None,
|
|
|
|
source: hir::GenericParamSource::Generics,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
));
|
2023-08-04 22:16:29 +00:00
|
|
|
debug!("lower_async_fn_ret_ty: generic_params={:#?}", generic_params);
|
|
|
|
|
2023-08-07 22:09:12 +00:00
|
|
|
let lifetime_mapping = self.arena.alloc_slice(&synthesized_lifetime_args);
|
2023-08-04 22:16:29 +00:00
|
|
|
|
|
|
|
let opaque_ty_item = hir::OpaqueTy {
|
|
|
|
generics: this.arena.alloc(hir::Generics {
|
|
|
|
params: generic_params,
|
|
|
|
predicates: &[],
|
|
|
|
has_where_clause_predicates: false,
|
|
|
|
where_clause_span: this.lower_span(span),
|
|
|
|
span: this.lower_span(span),
|
|
|
|
}),
|
|
|
|
bounds,
|
|
|
|
origin,
|
|
|
|
lifetime_mapping,
|
|
|
|
in_trait,
|
2024-04-04 10:48:47 -04:00
|
|
|
precise_capturing_args,
|
2023-08-04 22:16:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Generate an `type Foo = impl Trait;` declaration.
|
|
|
|
trace!("registering opaque type with id {:#?}", opaque_ty_def_id);
|
|
|
|
let opaque_ty_item = hir::Item {
|
|
|
|
owner_id: hir::OwnerId { def_id: opaque_ty_def_id },
|
|
|
|
ident: Ident::empty(),
|
|
|
|
kind: hir::ItemKind::OpaqueTy(this.arena.alloc(opaque_ty_item)),
|
|
|
|
vis_span: this.lower_span(span.shrink_to_lo()),
|
|
|
|
span: this.lower_span(opaque_ty_span),
|
|
|
|
};
|
|
|
|
|
|
|
|
hir::OwnerNode::Item(this.arena.alloc(opaque_ty_item))
|
|
|
|
});
|
|
|
|
|
|
|
|
let generic_args = self.arena.alloc_from_iter(
|
|
|
|
synthesized_lifetime_args
|
|
|
|
.iter()
|
|
|
|
.map(|(lifetime, _)| hir::GenericArg::Lifetime(*lifetime)),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Create the `Foo<...>` reference itself. Note that the `type
|
|
|
|
// Foo = impl Trait` is, internally, created as a child of the
|
|
|
|
// async fn, so the *type parameters* are inherited. It's
|
|
|
|
// only the lifetime parameters that we must supply.
|
|
|
|
hir::TyKind::OpaqueDef(
|
|
|
|
hir::ItemId { owner_id: hir::OwnerId { def_id: opaque_ty_def_id } },
|
|
|
|
generic_args,
|
|
|
|
in_trait,
|
|
|
|
)
|
2022-07-26 14:49:24 -03:00
|
|
|
}
|
|
|
|
|
2024-04-04 10:48:47 -04:00
|
|
|
fn lower_precise_capturing_args(
|
|
|
|
&mut self,
|
|
|
|
precise_capturing_args: &[PreciseCapturingArg],
|
|
|
|
) -> &'hir [hir::PreciseCapturingArg<'hir>] {
|
|
|
|
self.arena.alloc_from_iter(precise_capturing_args.iter().map(|arg| match arg {
|
|
|
|
PreciseCapturingArg::Lifetime(lt) => {
|
|
|
|
hir::PreciseCapturingArg::Lifetime(self.lower_lifetime(lt))
|
|
|
|
}
|
2024-04-04 20:23:52 -04:00
|
|
|
PreciseCapturingArg::Arg(path, id) => {
|
|
|
|
let [segment] = path.segments.as_slice() else {
|
|
|
|
panic!();
|
|
|
|
};
|
|
|
|
let res = self.resolver.get_partial_res(*id).map_or(Res::Err, |partial_res| {
|
2024-04-04 10:48:47 -04:00
|
|
|
partial_res.full_res().expect("no partial res expected for precise capture arg")
|
|
|
|
});
|
2024-04-04 14:46:26 -04:00
|
|
|
hir::PreciseCapturingArg::Param(hir::PreciseCapturingNonLifetimeArg {
|
2024-04-04 20:23:52 -04:00
|
|
|
hir_id: self.lower_node_id(*id),
|
|
|
|
ident: self.lower_ident(segment.ident),
|
2024-04-04 14:46:26 -04:00
|
|
|
res: self.lower_res(res),
|
|
|
|
})
|
2024-04-04 10:48:47 -04:00
|
|
|
}
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2019-12-01 17:21:00 +01:00
|
|
|
fn lower_fn_params_to_names(&mut self, decl: &FnDecl) -> &'hir [Ident] {
|
2023-10-25 17:44:17 +02:00
|
|
|
self.arena.alloc_from_iter(decl.inputs.iter().map(|param| match param.pat.kind {
|
2021-08-21 00:29:08 +03:00
|
|
|
PatKind::Ident(_, ident, _) => self.lower_ident(ident),
|
|
|
|
_ => Ident::new(kw::Empty, self.lower_span(param.pat.span)),
|
2019-12-01 17:21:00 +01:00
|
|
|
}))
|
2016-12-20 22:46:11 +02:00
|
|
|
}
|
|
|
|
|
2023-11-29 12:07:43 -08:00
|
|
|
/// Lowers a function declaration.
|
|
|
|
///
|
|
|
|
/// `decl`: the unlowered (AST) function declaration.
|
|
|
|
///
|
|
|
|
/// `fn_node_id`: `impl Trait` arguments are lowered into generic parameters on the given
|
|
|
|
/// `NodeId`.
|
|
|
|
///
|
|
|
|
/// `transform_return_type`: if `Some`, applies some conversion to the return type, such as is
|
2023-11-30 17:32:29 -08:00
|
|
|
/// needed for `async fn` and `gen fn`. See [`CoroutineKind`] for more details.
|
2022-08-31 13:01:10 +00:00
|
|
|
#[instrument(level = "debug", skip(self))]
|
2018-03-21 04:24:27 -04:00
|
|
|
fn lower_fn_decl(
|
|
|
|
&mut self,
|
|
|
|
decl: &FnDecl,
|
2022-11-06 09:33:52 +00:00
|
|
|
fn_node_id: NodeId,
|
2022-09-02 15:57:31 +00:00
|
|
|
fn_span: Span,
|
2022-01-11 19:00:34 -08:00
|
|
|
kind: FnDeclKind,
|
2023-11-30 16:39:56 -08:00
|
|
|
coro: Option<CoroutineKind>,
|
2019-12-01 11:22:58 +01:00
|
|
|
) -> &'hir hir::FnDecl<'hir> {
|
2019-08-10 14:38:17 +03:00
|
|
|
let c_variadic = decl.c_variadic();
|
|
|
|
|
2022-04-07 20:54:13 +02:00
|
|
|
// Skip the `...` (`CVarArgs`) trailing arguments from the AST,
|
|
|
|
// as they are not explicit in HIR/Ty function signatures.
|
|
|
|
// (instead, the `c_variadic` flag is set to `true`)
|
|
|
|
let mut inputs = &decl.inputs[..];
|
|
|
|
if c_variadic {
|
|
|
|
inputs = &inputs[..inputs.len() - 1];
|
|
|
|
}
|
|
|
|
let inputs = self.arena.alloc_from_iter(inputs.iter().map(|param| {
|
2024-01-07 02:48:38 +00:00
|
|
|
let itctx = match kind {
|
|
|
|
FnDeclKind::Fn | FnDeclKind::Inherent | FnDeclKind::Impl | FnDeclKind::Trait => {
|
|
|
|
ImplTraitContext::Universal
|
|
|
|
}
|
|
|
|
FnDeclKind::ExternFn => {
|
|
|
|
ImplTraitContext::Disallowed(ImplTraitPosition::ExternFnParam)
|
|
|
|
}
|
|
|
|
FnDeclKind::Closure => {
|
|
|
|
ImplTraitContext::Disallowed(ImplTraitPosition::ClosureParam)
|
|
|
|
}
|
|
|
|
FnDeclKind::Pointer => {
|
|
|
|
ImplTraitContext::Disallowed(ImplTraitPosition::PointerParam)
|
|
|
|
}
|
2022-11-06 09:33:52 +00:00
|
|
|
};
|
2024-02-07 19:27:44 +00:00
|
|
|
self.lower_ty_direct(¶m.ty, itctx)
|
2022-04-07 20:54:13 +02:00
|
|
|
}));
|
2018-06-06 15:50:59 -07:00
|
|
|
|
2023-11-30 14:54:39 -08:00
|
|
|
let output = match coro {
|
2023-11-30 16:39:56 -08:00
|
|
|
Some(coro) => {
|
2023-11-29 12:07:43 -08:00
|
|
|
let fn_def_id = self.local_def_id(fn_node_id);
|
2023-11-30 14:54:39 -08:00
|
|
|
self.lower_coroutine_fn_ret_ty(&decl.output, fn_def_id, coro, kind, fn_span)
|
2023-11-29 12:07:43 -08:00
|
|
|
}
|
2023-11-30 16:39:56 -08:00
|
|
|
None => match &decl.output {
|
2022-11-22 15:37:54 +00:00
|
|
|
FnRetTy::Ty(ty) => {
|
2024-01-07 02:48:38 +00:00
|
|
|
let itctx = match kind {
|
|
|
|
FnDeclKind::Fn
|
|
|
|
| FnDeclKind::Inherent
|
|
|
|
| FnDeclKind::Trait
|
2024-03-06 18:44:55 +00:00
|
|
|
| FnDeclKind::Impl => ImplTraitContext::OpaqueTy {
|
2024-01-07 02:48:38 +00:00
|
|
|
origin: hir::OpaqueTyOrigin::FnReturn(self.local_def_id(fn_node_id)),
|
2024-03-06 18:44:55 +00:00
|
|
|
fn_kind: Some(kind),
|
2024-01-07 02:48:38 +00:00
|
|
|
},
|
|
|
|
FnDeclKind::ExternFn => {
|
|
|
|
ImplTraitContext::Disallowed(ImplTraitPosition::ExternFnReturn)
|
|
|
|
}
|
|
|
|
FnDeclKind::Closure => {
|
|
|
|
ImplTraitContext::Disallowed(ImplTraitPosition::ClosureReturn)
|
|
|
|
}
|
|
|
|
FnDeclKind::Pointer => {
|
|
|
|
ImplTraitContext::Disallowed(ImplTraitPosition::PointerReturn)
|
2022-08-31 04:03:24 +00:00
|
|
|
}
|
2019-12-29 14:23:20 +00:00
|
|
|
};
|
2024-02-07 19:27:44 +00:00
|
|
|
hir::FnRetTy::Return(self.lower_ty(ty, itctx))
|
2019-12-29 14:23:20 +00:00
|
|
|
}
|
2022-11-22 15:37:54 +00:00
|
|
|
FnRetTy::Default(span) => hir::FnRetTy::DefaultReturn(self.lower_span(*span)),
|
2023-11-29 12:07:43 -08:00
|
|
|
},
|
2018-06-06 15:50:59 -07:00
|
|
|
};
|
|
|
|
|
2019-12-01 11:22:58 +01:00
|
|
|
self.arena.alloc(hir::FnDecl {
|
2018-06-06 15:50:59 -07:00
|
|
|
inputs,
|
|
|
|
output,
|
2019-08-10 14:38:17 +03:00
|
|
|
c_variadic,
|
2022-11-06 09:33:52 +00:00
|
|
|
lifetime_elision_allowed: self.resolver.lifetime_elision_allowed.contains(&fn_node_id),
|
2019-12-24 17:38:22 -05:00
|
|
|
implicit_self: decl.inputs.get(0).map_or(hir::ImplicitSelfKind::None, |arg| {
|
2020-12-29 01:20:06 +01:00
|
|
|
let is_mutable_pat = matches!(
|
|
|
|
arg.pat.kind,
|
2024-04-16 19:23:30 -04:00
|
|
|
PatKind::Ident(hir::BindingMode(_, Mutability::Mut), ..)
|
2020-12-29 01:20:06 +01:00
|
|
|
);
|
2018-10-01 17:46:04 +02:00
|
|
|
|
2022-11-22 15:37:54 +00:00
|
|
|
match &arg.ty.kind {
|
2019-12-24 17:38:22 -05:00
|
|
|
TyKind::ImplicitSelf if is_mutable_pat => hir::ImplicitSelfKind::Mut,
|
|
|
|
TyKind::ImplicitSelf => hir::ImplicitSelfKind::Imm,
|
|
|
|
// Given we are only considering `ImplicitSelf` types, we needn't consider
|
|
|
|
// the case where we have a mutable pattern to a reference as that would
|
|
|
|
// no longer be an `ImplicitSelf`.
|
2022-12-28 18:06:11 +01:00
|
|
|
TyKind::Ref(_, mt) if mt.ty.kind.is_implicit_self() => match mt.mutbl {
|
2024-03-23 21:04:45 -04:00
|
|
|
hir::Mutability::Not => hir::ImplicitSelfKind::RefImm,
|
|
|
|
hir::Mutability::Mut => hir::ImplicitSelfKind::RefMut,
|
2022-11-24 18:13:30 +00:00
|
|
|
},
|
2019-12-24 17:38:22 -05:00
|
|
|
_ => hir::ImplicitSelfKind::None,
|
|
|
|
}
|
|
|
|
}),
|
2016-05-10 01:11:59 +00:00
|
|
|
})
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2019-08-02 00:08:05 +01:00
|
|
|
// Transforms `-> T` for `async fn` into `-> OpaqueTy { .. }`
|
|
|
|
// combined with the following definition of `OpaqueTy`:
|
2019-03-13 17:42:23 -07:00
|
|
|
//
|
2019-08-02 00:08:05 +01:00
|
|
|
// type OpaqueTy<generics_from_parent_fn> = impl Future<Output = T>;
|
2018-06-06 15:50:59 -07:00
|
|
|
//
|
2019-02-28 22:43:53 +00:00
|
|
|
// `output`: unlowered output type (`T` in `-> T`)
|
2022-12-06 14:32:14 +00:00
|
|
|
// `fn_node_id`: `NodeId` of the parent function (used to create child impl trait definition)
|
2019-08-02 00:08:05 +01:00
|
|
|
// `opaque_ty_node_id`: `NodeId` of the opaque `impl Trait` type that should be created
|
2022-08-31 13:01:10 +00:00
|
|
|
#[instrument(level = "debug", skip(self))]
|
2023-11-29 12:07:43 -08:00
|
|
|
fn lower_coroutine_fn_ret_ty(
|
2018-06-06 15:50:59 -07:00
|
|
|
&mut self,
|
2020-02-15 12:10:59 +09:00
|
|
|
output: &FnRetTy,
|
2023-08-04 22:16:29 +00:00
|
|
|
fn_def_id: LocalDefId,
|
2023-11-30 14:54:39 -08:00
|
|
|
coro: CoroutineKind,
|
2023-08-04 23:54:14 +00:00
|
|
|
fn_kind: FnDeclKind,
|
2023-10-02 21:31:46 +00:00
|
|
|
fn_span: Span,
|
2020-02-15 12:10:59 +09:00
|
|
|
) -> hir::FnRetTy<'hir> {
|
2023-10-02 21:31:46 +00:00
|
|
|
let span = self.lower_span(fn_span);
|
2018-06-06 15:50:59 -07:00
|
|
|
|
2023-12-07 17:39:02 +00:00
|
|
|
let (opaque_ty_node_id, allowed_features) = match coro {
|
|
|
|
CoroutineKind::Async { return_impl_trait_id, .. } => (return_impl_trait_id, None),
|
|
|
|
CoroutineKind::Gen { return_impl_trait_id, .. } => (return_impl_trait_id, None),
|
|
|
|
CoroutineKind::AsyncGen { return_impl_trait_id, .. } => {
|
|
|
|
(return_impl_trait_id, Some(self.allow_async_iterator.clone()))
|
|
|
|
}
|
2023-11-29 12:07:43 -08:00
|
|
|
};
|
|
|
|
|
2023-12-07 17:39:02 +00:00
|
|
|
let opaque_ty_span =
|
|
|
|
self.mark_span_with_reason(DesugaringKind::Async, span, allowed_features);
|
|
|
|
|
2024-04-04 10:48:47 -04:00
|
|
|
let captured_lifetimes = self
|
2023-08-04 22:16:29 +00:00
|
|
|
.resolver
|
|
|
|
.take_extra_lifetime_params(opaque_ty_node_id)
|
|
|
|
.into_iter()
|
|
|
|
.map(|(ident, id, _)| Lifetime { id, ident })
|
2023-06-22 17:56:46 -03:00
|
|
|
.collect();
|
2022-07-26 15:11:15 -03:00
|
|
|
|
2023-08-04 22:16:29 +00:00
|
|
|
let opaque_ty_ref = self.lower_opaque_inner(
|
|
|
|
opaque_ty_node_id,
|
|
|
|
hir::OpaqueTyOrigin::AsyncFn(fn_def_id),
|
2023-08-04 23:54:14 +00:00
|
|
|
matches!(fn_kind, FnDeclKind::Trait),
|
2023-08-04 22:16:29 +00:00
|
|
|
captured_lifetimes,
|
|
|
|
span,
|
|
|
|
opaque_ty_span,
|
2024-04-04 10:48:47 -04:00
|
|
|
None,
|
2023-08-04 22:16:29 +00:00
|
|
|
|this| {
|
2023-12-04 13:43:38 -08:00
|
|
|
let bound = this.lower_coroutine_fn_output_type_to_bound(
|
2022-09-02 17:54:58 +00:00
|
|
|
output,
|
2023-11-30 14:54:39 -08:00
|
|
|
coro,
|
2023-12-07 17:39:02 +00:00
|
|
|
opaque_ty_span,
|
2024-03-06 18:44:55 +00:00
|
|
|
ImplTraitContext::OpaqueTy {
|
2023-09-13 16:04:42 +00:00
|
|
|
origin: hir::OpaqueTyOrigin::FnReturn(fn_def_id),
|
2024-03-06 18:44:55 +00:00
|
|
|
fn_kind: Some(fn_kind),
|
2022-09-02 17:54:58 +00:00
|
|
|
},
|
|
|
|
);
|
2023-12-04 13:43:38 -08:00
|
|
|
arena_vec![this; bound]
|
2023-08-04 22:16:29 +00:00
|
|
|
},
|
2023-06-21 19:09:18 -03:00
|
|
|
);
|
2018-06-21 22:24:24 -07:00
|
|
|
|
2019-12-01 10:25:45 +01:00
|
|
|
let opaque_ty = self.ty(opaque_ty_span, opaque_ty_ref);
|
2020-02-15 12:10:59 +09:00
|
|
|
hir::FnRetTy::Return(self.arena.alloc(opaque_ty))
|
2019-03-13 17:42:23 -07:00
|
|
|
}
|
2018-06-06 15:50:59 -07:00
|
|
|
|
2021-03-06 22:07:38 -08:00
|
|
|
/// Transforms `-> T` into `Future<Output = T>`.
|
2023-12-04 13:43:38 -08:00
|
|
|
fn lower_coroutine_fn_output_type_to_bound(
|
2019-03-13 17:42:23 -07:00
|
|
|
&mut self,
|
2020-02-15 12:10:59 +09:00
|
|
|
output: &FnRetTy,
|
2023-11-30 14:54:39 -08:00
|
|
|
coro: CoroutineKind,
|
2023-12-07 17:39:02 +00:00
|
|
|
opaque_ty_span: Span,
|
2024-02-07 19:27:44 +00:00
|
|
|
itctx: ImplTraitContext,
|
2019-11-30 17:46:46 +01:00
|
|
|
) -> hir::GenericBound<'hir> {
|
2019-03-13 17:42:23 -07:00
|
|
|
// Compute the `T` in `Future<Output = T>` from the return type.
|
|
|
|
let output_ty = match output {
|
2020-02-15 12:10:59 +09:00
|
|
|
FnRetTy::Ty(ty) => {
|
2019-12-29 14:23:20 +00:00
|
|
|
// Not `OpaqueTyOrigin::AsyncFn`: that's only used for the
|
|
|
|
// `impl Future` opaque type that `async fn` implicitly
|
|
|
|
// generates.
|
2024-02-07 19:27:44 +00:00
|
|
|
self.lower_ty(ty, itctx)
|
2019-12-29 14:23:20 +00:00
|
|
|
}
|
2020-02-15 12:10:59 +09:00
|
|
|
FnRetTy::Default(ret_ty_span) => self.arena.alloc(self.ty_tup(*ret_ty_span, &[])),
|
2019-03-13 17:42:23 -07:00
|
|
|
};
|
2018-06-06 15:50:59 -07:00
|
|
|
|
2023-12-04 13:43:38 -08:00
|
|
|
// "<$assoc_ty_name = T>"
|
|
|
|
let (assoc_ty_name, trait_lang_item) = match coro {
|
2023-12-05 21:45:01 +00:00
|
|
|
CoroutineKind::Async { .. } => (sym::Output, hir::LangItem::Future),
|
|
|
|
CoroutineKind::Gen { .. } => (sym::Item, hir::LangItem::Iterator),
|
|
|
|
CoroutineKind::AsyncGen { .. } => (sym::Item, hir::LangItem::AsyncIterator),
|
2023-11-29 12:07:43 -08:00
|
|
|
};
|
|
|
|
|
2023-11-23 04:55:03 +00:00
|
|
|
let bound_args = self.arena.alloc(hir::GenericArgs {
|
2019-12-01 17:10:12 +01:00
|
|
|
args: &[],
|
2023-12-07 17:39:02 +00:00
|
|
|
bindings: arena_vec![self; self.assoc_ty_binding(assoc_ty_name, opaque_ty_span, output_ty)],
|
2023-03-16 22:00:08 +00:00
|
|
|
parenthesized: hir::GenericArgsParentheses::No,
|
2021-05-12 11:36:07 +02:00
|
|
|
span_ext: DUMMY_SP,
|
2018-06-06 15:50:59 -07:00
|
|
|
});
|
|
|
|
|
2023-11-23 04:55:03 +00:00
|
|
|
hir::GenericBound::Trait(
|
|
|
|
hir::PolyTraitRef {
|
|
|
|
bound_generic_params: &[],
|
|
|
|
trait_ref: hir::TraitRef {
|
|
|
|
path: self.make_lang_item_path(
|
|
|
|
trait_lang_item,
|
|
|
|
opaque_ty_span,
|
|
|
|
Some(bound_args),
|
|
|
|
),
|
|
|
|
hir_ref_id: self.next_id(),
|
|
|
|
},
|
|
|
|
span: opaque_ty_span,
|
|
|
|
},
|
|
|
|
hir::TraitBoundModifier::None,
|
2019-03-13 17:42:23 -07:00
|
|
|
)
|
2018-06-06 15:50:59 -07:00
|
|
|
}
|
|
|
|
|
2022-05-20 15:52:56 -03:00
|
|
|
#[instrument(level = "trace", skip(self))]
|
2018-05-28 13:33:28 +01:00
|
|
|
fn lower_param_bound(
|
2018-03-21 04:24:27 -04:00
|
|
|
&mut self,
|
2018-06-14 12:08:58 +01:00
|
|
|
tpb: &GenericBound,
|
2024-02-07 19:27:44 +00:00
|
|
|
itctx: ImplTraitContext,
|
2019-11-30 17:46:46 +01:00
|
|
|
) -> hir::GenericBound<'hir> {
|
2021-07-23 18:47:53 -07:00
|
|
|
match tpb {
|
2023-12-20 15:22:06 +01:00
|
|
|
GenericBound::Trait(p, modifiers) => hir::GenericBound::Trait(
|
2024-01-26 17:00:28 +00:00
|
|
|
self.lower_poly_trait_ref(p, itctx, *modifiers),
|
2023-12-20 15:22:06 +01:00
|
|
|
self.lower_trait_bound_modifiers(*modifiers),
|
2019-12-24 17:38:22 -05:00
|
|
|
),
|
2021-07-23 18:47:53 -07:00
|
|
|
GenericBound::Outlives(lifetime) => {
|
2022-07-26 15:50:25 -03:00
|
|
|
hir::GenericBound::Outlives(self.lower_lifetime(lifetime))
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2022-09-01 12:06:48 +10:00
|
|
|
fn lower_lifetime(&mut self, l: &Lifetime) -> &'hir hir::Lifetime {
|
2022-04-07 20:54:13 +02:00
|
|
|
let ident = self.lower_ident(l.ident);
|
2022-11-05 22:41:07 +00:00
|
|
|
self.new_named_lifetime(l.id, l.id, ident)
|
2018-03-21 17:12:39 -04:00
|
|
|
}
|
2017-11-16 22:59:45 -08:00
|
|
|
|
2022-08-31 13:01:10 +00:00
|
|
|
#[instrument(level = "debug", skip(self))]
|
2022-04-07 20:54:13 +02:00
|
|
|
fn new_named_lifetime_with_res(
|
2018-03-21 17:12:39 -04:00
|
|
|
&mut self,
|
|
|
|
id: NodeId,
|
2022-04-07 20:54:13 +02:00
|
|
|
ident: Ident,
|
|
|
|
res: LifetimeRes,
|
2022-09-01 12:06:48 +10:00
|
|
|
) -> &'hir hir::Lifetime {
|
2022-11-05 22:41:07 +00:00
|
|
|
let res = match res {
|
2022-08-02 20:01:40 -03:00
|
|
|
LifetimeRes::Param { param, .. } => {
|
2022-08-14 18:25:19 +02:00
|
|
|
let param = self.get_remapped_def_id(param);
|
2022-11-05 22:41:07 +00:00
|
|
|
hir::LifetimeName::Param(param)
|
2022-04-07 20:54:13 +02:00
|
|
|
}
|
2022-08-02 15:27:52 -03:00
|
|
|
LifetimeRes::Fresh { param, .. } => {
|
2022-08-02 20:01:40 -03:00
|
|
|
let param = self.local_def_id(param);
|
2022-11-05 22:41:07 +00:00
|
|
|
hir::LifetimeName::Param(param)
|
2022-04-07 20:54:13 +02:00
|
|
|
}
|
2022-06-13 08:22:06 +02:00
|
|
|
LifetimeRes::Infer => hir::LifetimeName::Infer,
|
2022-04-07 20:54:13 +02:00
|
|
|
LifetimeRes::Static => hir::LifetimeName::Static,
|
|
|
|
LifetimeRes::Error => hir::LifetimeName::Error,
|
2022-11-05 22:41:07 +00:00
|
|
|
res => panic!(
|
|
|
|
"Unexpected lifetime resolution {:?} for {:?} at {:?}",
|
|
|
|
res, ident, ident.span
|
|
|
|
),
|
2022-04-07 20:54:13 +02:00
|
|
|
};
|
2022-08-02 20:01:40 -03:00
|
|
|
|
2022-11-05 22:41:07 +00:00
|
|
|
debug!(?res);
|
2022-09-01 12:06:48 +10:00
|
|
|
self.arena.alloc(hir::Lifetime {
|
|
|
|
hir_id: self.lower_node_id(id),
|
2022-11-05 22:41:07 +00:00
|
|
|
ident: self.lower_ident(ident),
|
|
|
|
res,
|
2022-09-01 12:06:48 +10:00
|
|
|
})
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2022-08-31 13:01:10 +00:00
|
|
|
#[instrument(level = "debug", skip(self))]
|
2022-08-02 23:06:38 -03:00
|
|
|
fn new_named_lifetime(
|
|
|
|
&mut self,
|
|
|
|
id: NodeId,
|
|
|
|
new_id: NodeId,
|
|
|
|
ident: Ident,
|
2022-09-01 12:06:48 +10:00
|
|
|
) -> &'hir hir::Lifetime {
|
2022-08-02 23:06:38 -03:00
|
|
|
let res = self.resolver.get_lifetime_res(id).unwrap_or(LifetimeRes::Error);
|
2022-11-05 22:41:07 +00:00
|
|
|
self.new_named_lifetime_with_res(new_id, ident, res)
|
2022-08-02 23:06:38 -03:00
|
|
|
}
|
|
|
|
|
2019-12-26 17:57:50 +01:00
|
|
|
fn lower_generic_params_mut<'s>(
|
|
|
|
&'s mut self,
|
|
|
|
params: &'s [GenericParam],
|
2023-02-28 06:26:48 +00:00
|
|
|
source: hir::GenericParamSource,
|
2019-12-26 17:57:50 +01:00
|
|
|
) -> impl Iterator<Item = hir::GenericParam<'hir>> + Captures<'a> + Captures<'s> {
|
2023-02-28 06:26:48 +00:00
|
|
|
params.iter().map(move |param| self.lower_generic_param(param, source))
|
2019-12-24 17:38:22 -05:00
|
|
|
}
|
|
|
|
|
2023-02-28 06:26:48 +00:00
|
|
|
fn lower_generic_params(
|
|
|
|
&mut self,
|
|
|
|
params: &[GenericParam],
|
|
|
|
source: hir::GenericParamSource,
|
|
|
|
) -> &'hir [hir::GenericParam<'hir>] {
|
|
|
|
self.arena.alloc_from_iter(self.lower_generic_params_mut(params, source))
|
2019-12-01 17:10:12 +01:00
|
|
|
}
|
|
|
|
|
2022-05-20 15:52:56 -03:00
|
|
|
#[instrument(level = "trace", skip(self))]
|
2023-02-28 06:26:48 +00:00
|
|
|
fn lower_generic_param(
|
|
|
|
&mut self,
|
|
|
|
param: &GenericParam,
|
|
|
|
source: hir::GenericParamSource,
|
|
|
|
) -> hir::GenericParam<'hir> {
|
2024-01-01 19:29:27 +01:00
|
|
|
let (name, kind) = self.lower_generic_param_kind(param, source);
|
2022-05-20 17:32:48 -03:00
|
|
|
|
|
|
|
let hir_id = self.lower_node_id(param.id);
|
|
|
|
self.lower_attrs(hir_id, ¶m.attrs);
|
|
|
|
hir::GenericParam {
|
|
|
|
hir_id,
|
2022-11-06 18:26:36 +00:00
|
|
|
def_id: self.local_def_id(param.id),
|
2022-05-20 17:32:48 -03:00
|
|
|
name,
|
|
|
|
span: self.lower_span(param.span()),
|
2023-03-19 21:32:34 +04:00
|
|
|
pure_wrt_drop: attr::contains_name(¶m.attrs, sym::may_dangle),
|
2022-05-20 17:32:48 -03:00
|
|
|
kind,
|
|
|
|
colon_span: param.colon_span.map(|s| self.lower_span(s)),
|
2023-02-28 06:26:48 +00:00
|
|
|
source,
|
2022-05-20 17:32:48 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn lower_generic_param_kind(
|
|
|
|
&mut self,
|
|
|
|
param: &GenericParam,
|
2024-01-01 19:29:27 +01:00
|
|
|
source: hir::GenericParamSource,
|
2022-05-20 17:32:48 -03:00
|
|
|
) -> (hir::ParamName, hir::GenericParamKind<'hir>) {
|
2022-11-22 15:37:54 +00:00
|
|
|
match ¶m.kind {
|
2018-05-30 16:49:39 +01:00
|
|
|
GenericParamKind::Lifetime => {
|
2022-05-27 11:29:18 +02:00
|
|
|
// AST resolution emitted an error on those parameters, so we lower them using
|
|
|
|
// `ParamName::Error`.
|
|
|
|
let param_name =
|
|
|
|
if let Some(LifetimeRes::Error) = self.resolver.get_lifetime_res(param.id) {
|
|
|
|
ParamName::Error
|
|
|
|
} else {
|
|
|
|
let ident = self.lower_ident(param.ident);
|
|
|
|
ParamName::Plain(ident)
|
|
|
|
};
|
2019-12-24 17:38:22 -05:00
|
|
|
let kind =
|
|
|
|
hir::GenericParamKind::Lifetime { kind: hir::LifetimeParamKind::Explicit };
|
2017-11-16 22:59:45 -08:00
|
|
|
|
2019-02-05 16:52:02 +01:00
|
|
|
(param_name, kind)
|
2018-05-26 00:27:54 +01:00
|
|
|
}
|
2022-11-22 15:37:54 +00:00
|
|
|
GenericParamKind::Type { default, .. } => {
|
2024-01-01 19:29:27 +01:00
|
|
|
// Not only do we deny type param defaults in binders but we also map them to `None`
|
|
|
|
// since later compiler stages cannot handle them (and shouldn't need to be able to).
|
|
|
|
let default = default
|
|
|
|
.as_ref()
|
|
|
|
.filter(|_| match source {
|
|
|
|
hir::GenericParamSource::Generics => true,
|
|
|
|
hir::GenericParamSource::Binder => {
|
|
|
|
self.dcx().emit_err(errors::GenericParamDefaultInBinder {
|
|
|
|
span: param.span(),
|
|
|
|
});
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.map(|def| {
|
2023-02-14 22:15:32 +00:00
|
|
|
self.lower_ty(
|
2024-01-01 19:29:27 +01:00
|
|
|
def,
|
2024-02-07 19:27:44 +00:00
|
|
|
ImplTraitContext::Disallowed(ImplTraitPosition::GenericDefault),
|
2023-02-14 22:15:32 +00:00
|
|
|
)
|
2024-01-01 19:29:27 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
let kind = hir::GenericParamKind::Type { default, synthetic: false };
|
2019-02-05 16:52:02 +01:00
|
|
|
|
2021-08-21 00:29:08 +03:00
|
|
|
(hir::ParamName::Plain(self.lower_ident(param.ident)), kind)
|
2018-05-26 00:27:54 +01:00
|
|
|
}
|
2022-11-22 15:37:54 +00:00
|
|
|
GenericParamKind::Const { ty, kw_span: _, default } => {
|
2023-11-21 20:07:32 +01:00
|
|
|
let ty = self
|
2024-02-07 19:27:44 +00:00
|
|
|
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::GenericDefault));
|
2024-01-01 19:29:27 +01:00
|
|
|
|
|
|
|
// Not only do we deny const param defaults in binders but we also map them to `None`
|
|
|
|
// since later compiler stages cannot handle them (and shouldn't need to be able to).
|
|
|
|
let default = default
|
|
|
|
.as_ref()
|
|
|
|
.filter(|_| match source {
|
|
|
|
hir::GenericParamSource::Generics => true,
|
|
|
|
hir::GenericParamSource::Binder => {
|
|
|
|
self.dcx().emit_err(errors::GenericParamDefaultInBinder {
|
|
|
|
span: param.span(),
|
|
|
|
});
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.map(|def| self.lower_anon_const(def));
|
|
|
|
|
2021-08-21 00:29:08 +03:00
|
|
|
(
|
|
|
|
hir::ParamName::Plain(self.lower_ident(param.ident)),
|
2023-12-04 14:22:05 +00:00
|
|
|
hir::GenericParamKind::Const { ty, default, is_host_effect: false },
|
2021-08-21 00:29:08 +03:00
|
|
|
)
|
2020-01-12 15:55:12 +13:00
|
|
|
}
|
2018-05-26 00:27:54 +01:00
|
|
|
}
|
2015-10-25 18:33:51 +03:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2023-07-25 05:58:53 +00:00
|
|
|
fn lower_trait_ref(
|
|
|
|
&mut self,
|
2024-01-26 17:00:28 +00:00
|
|
|
modifiers: ast::TraitBoundModifiers,
|
2023-07-25 05:58:53 +00:00
|
|
|
p: &TraitRef,
|
2024-02-07 19:27:44 +00:00
|
|
|
itctx: ImplTraitContext,
|
2023-07-25 05:58:53 +00:00
|
|
|
) -> hir::TraitRef<'hir> {
|
|
|
|
let path = match self.lower_qpath(
|
|
|
|
p.ref_id,
|
|
|
|
&None,
|
|
|
|
&p.path,
|
|
|
|
ParamMode::Explicit,
|
|
|
|
itctx,
|
2024-01-26 17:00:28 +00:00
|
|
|
Some(modifiers),
|
2023-07-25 05:58:53 +00:00
|
|
|
) {
|
2019-06-19 20:10:37 +03:00
|
|
|
hir::QPath::Resolved(None, path) => path,
|
2022-12-19 10:31:55 +01:00
|
|
|
qpath => panic!("lower_trait_ref: unexpected QPath `{qpath:?}`"),
|
2017-01-09 17:46:11 +02:00
|
|
|
};
|
2019-12-24 17:38:22 -05:00
|
|
|
hir::TraitRef { path, hir_ref_id: self.lower_node_id(p.ref_id) }
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 13:01:10 +00:00
|
|
|
#[instrument(level = "debug", skip(self))]
|
2018-03-21 04:24:27 -04:00
|
|
|
fn lower_poly_trait_ref(
|
|
|
|
&mut self,
|
|
|
|
p: &PolyTraitRef,
|
2024-02-07 19:27:44 +00:00
|
|
|
itctx: ImplTraitContext,
|
2024-01-26 17:00:28 +00:00
|
|
|
modifiers: ast::TraitBoundModifiers,
|
2019-11-30 17:46:46 +01:00
|
|
|
) -> hir::PolyTraitRef<'hir> {
|
2022-09-14 17:14:36 -03:00
|
|
|
let bound_generic_params =
|
|
|
|
self.lower_lifetime_binder(p.trait_ref.ref_id, &p.bound_generic_params);
|
2024-01-26 17:00:28 +00:00
|
|
|
let trait_ref = self.lower_trait_ref(modifiers, &p.trait_ref, itctx);
|
2022-09-14 17:14:36 -03:00
|
|
|
hir::PolyTraitRef { bound_generic_params, trait_ref, span: self.lower_span(p.span) }
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
|
|
|
|
2024-02-07 19:27:44 +00:00
|
|
|
fn lower_mt(&mut self, mt: &MutTy, itctx: ImplTraitContext) -> hir::MutTy<'hir> {
|
2022-07-26 15:50:25 -03:00
|
|
|
hir::MutTy { ty: self.lower_ty(&mt.ty, itctx), mutbl: mt.mutbl }
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
|
2022-09-06 17:24:36 -03:00
|
|
|
#[instrument(level = "debug", skip(self), ret)]
|
2019-12-24 17:38:22 -05:00
|
|
|
fn lower_param_bounds(
|
|
|
|
&mut self,
|
|
|
|
bounds: &[GenericBound],
|
2024-02-07 19:27:44 +00:00
|
|
|
itctx: ImplTraitContext,
|
2019-11-30 17:46:46 +01:00
|
|
|
) -> hir::GenericBounds<'hir> {
|
2022-07-26 15:50:25 -03:00
|
|
|
self.arena.alloc_from_iter(self.lower_param_bounds_mut(bounds, itctx))
|
2019-12-01 11:22:58 +01:00
|
|
|
}
|
|
|
|
|
2022-09-14 17:40:51 -03:00
|
|
|
fn lower_param_bounds_mut<'s>(
|
2019-12-26 17:36:54 +01:00
|
|
|
&'s mut self,
|
|
|
|
bounds: &'s [GenericBound],
|
2024-02-07 19:27:44 +00:00
|
|
|
itctx: ImplTraitContext,
|
2022-09-14 17:40:51 -03:00
|
|
|
) -> impl Iterator<Item = hir::GenericBound<'hir>> + Captures<'s> + Captures<'a> {
|
2022-07-26 15:50:25 -03:00
|
|
|
bounds.iter().map(move |bound| self.lower_param_bound(bound, itctx))
|
2015-11-17 17:32:12 -05:00
|
|
|
}
|
|
|
|
|
2022-09-06 17:24:36 -03:00
|
|
|
#[instrument(level = "debug", skip(self), ret)]
|
2023-02-28 06:26:48 +00:00
|
|
|
fn lower_universal_param_and_bounds(
|
2022-06-07 18:17:41 -03:00
|
|
|
&mut self,
|
|
|
|
node_id: NodeId,
|
|
|
|
span: Span,
|
|
|
|
ident: Ident,
|
|
|
|
bounds: &[GenericBound],
|
|
|
|
) -> (hir::GenericParam<'hir>, Option<hir::WherePredicate<'hir>>, hir::TyKind<'hir>) {
|
|
|
|
// Add a definition for the in-band `Param`.
|
2021-07-18 20:09:20 +02:00
|
|
|
let def_id = self.local_def_id(node_id);
|
2022-12-25 22:16:04 +01:00
|
|
|
let span = self.lower_span(span);
|
2022-06-07 18:17:41 -03:00
|
|
|
|
|
|
|
// Set the name to `impl Bound1 + Bound2`.
|
|
|
|
let param = hir::GenericParam {
|
|
|
|
hir_id: self.lower_node_id(node_id),
|
2022-11-06 18:26:36 +00:00
|
|
|
def_id,
|
2022-06-07 18:17:41 -03:00
|
|
|
name: ParamName::Plain(self.lower_ident(ident)),
|
|
|
|
pure_wrt_drop: false,
|
2022-12-25 22:16:04 +01:00
|
|
|
span,
|
2022-06-07 18:17:41 -03:00
|
|
|
kind: hir::GenericParamKind::Type { default: None, synthetic: true },
|
|
|
|
colon_span: None,
|
2023-02-28 06:26:48 +00:00
|
|
|
source: hir::GenericParamSource::Generics,
|
2022-06-07 18:17:41 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
let preds = self.lower_generic_bound_predicate(
|
|
|
|
ident,
|
|
|
|
node_id,
|
|
|
|
&GenericParamKind::Type { default: None },
|
2022-07-19 18:10:27 -03:00
|
|
|
bounds,
|
2022-12-25 22:16:04 +01:00
|
|
|
/* colon_span */ None,
|
|
|
|
span,
|
2024-02-07 19:27:44 +00:00
|
|
|
ImplTraitContext::Universal,
|
2022-06-07 18:17:41 -03:00
|
|
|
hir::PredicateOrigin::ImplTrait,
|
|
|
|
);
|
|
|
|
|
2022-08-30 16:54:42 +10:00
|
|
|
let hir_id = self.next_id();
|
2022-08-30 15:10:28 +10:00
|
|
|
let res = Res::Def(DefKind::TyParam, def_id.to_def_id());
|
2022-06-07 18:17:41 -03:00
|
|
|
let ty = hir::TyKind::Path(hir::QPath::Resolved(
|
|
|
|
None,
|
|
|
|
self.arena.alloc(hir::Path {
|
2022-12-25 22:16:04 +01:00
|
|
|
span,
|
2022-08-30 15:10:28 +10:00
|
|
|
res,
|
2022-09-01 08:44:20 +10:00
|
|
|
segments:
|
|
|
|
arena_vec![self; hir::PathSegment::new(self.lower_ident(ident), hir_id, res)],
|
2022-06-07 18:17:41 -03:00
|
|
|
}),
|
|
|
|
));
|
|
|
|
|
|
|
|
(param, preds, ty)
|
|
|
|
}
|
|
|
|
|
2019-09-17 04:39:21 -04:00
|
|
|
/// Lowers a block directly to an expression, presuming that it
|
|
|
|
/// has no attributes and is not targeted by a `break`.
|
2019-11-29 13:43:03 +01:00
|
|
|
fn lower_block_expr(&mut self, b: &Block) -> hir::Expr<'hir> {
|
2019-09-17 04:39:21 -04:00
|
|
|
let block = self.lower_block(b, false);
|
2022-12-06 01:01:42 +00:00
|
|
|
self.expr_block(block)
|
2019-09-17 04:39:21 -04:00
|
|
|
}
|
|
|
|
|
2024-02-20 14:12:50 +11:00
|
|
|
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
|
2021-12-23 10:01:51 +01:00
|
|
|
fn lower_array_length(&mut self, c: &AnonConst) -> hir::ArrayLen {
|
|
|
|
match c.value.kind {
|
|
|
|
ExprKind::Underscore => {
|
2022-05-02 20:32:17 +02:00
|
|
|
if self.tcx.features().generic_arg_infer {
|
2024-01-27 18:59:20 +03:00
|
|
|
hir::ArrayLen::Infer(hir::InferArg {
|
|
|
|
hir_id: self.lower_node_id(c.id),
|
|
|
|
span: self.lower_span(c.value.span),
|
|
|
|
})
|
2021-12-23 10:01:51 +01:00
|
|
|
} else {
|
|
|
|
feature_err(
|
2024-01-10 00:37:30 -05:00
|
|
|
&self.tcx.sess,
|
2021-12-23 10:01:51 +01:00
|
|
|
sym::generic_arg_infer,
|
|
|
|
c.value.span,
|
|
|
|
"using `_` for array lengths is unstable",
|
|
|
|
)
|
2022-08-16 00:16:14 +09:00
|
|
|
.stash(c.value.span, StashKey::UnderscoreForArrayLengths);
|
2021-12-23 10:01:51 +01:00
|
|
|
hir::ArrayLen::Body(self.lower_anon_const(c))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => hir::ArrayLen::Body(self.lower_anon_const(c)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-17 21:28:50 +03:00
|
|
|
fn lower_anon_const(&mut self, c: &AnonConst) -> hir::AnonConst {
|
2023-11-28 19:06:23 +00:00
|
|
|
self.with_new_scopes(c.value.span, |this| hir::AnonConst {
|
2022-11-06 19:17:57 +00:00
|
|
|
def_id: this.local_def_id(c.id),
|
2019-12-24 17:38:22 -05:00
|
|
|
hir_id: this.lower_node_id(c.id),
|
|
|
|
body: this.lower_const_body(c.value.span, Some(&c.value)),
|
2018-07-20 15:59:00 +02:00
|
|
|
})
|
2018-05-17 21:28:50 +03:00
|
|
|
}
|
|
|
|
|
2016-05-10 01:11:59 +00:00
|
|
|
fn lower_unsafe_source(&mut self, u: UnsafeSource) -> hir::UnsafeSource {
|
|
|
|
match u {
|
2020-01-05 01:50:05 +01:00
|
|
|
CompilerGenerated => hir::UnsafeSource::CompilerGenerated,
|
|
|
|
UserProvided => hir::UnsafeSource::UserProvided,
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
|
2023-12-20 15:22:06 +01:00
|
|
|
fn lower_trait_bound_modifiers(
|
|
|
|
&mut self,
|
|
|
|
modifiers: TraitBoundModifiers,
|
|
|
|
) -> hir::TraitBoundModifier {
|
2023-12-18 17:55:55 +01:00
|
|
|
// Invalid modifier combinations will cause an error during AST validation.
|
|
|
|
// Arbitrarily pick a placeholder for them to make compilation proceed.
|
2023-12-20 15:22:06 +01:00
|
|
|
match (modifiers.constness, modifiers.polarity) {
|
|
|
|
(BoundConstness::Never, BoundPolarity::Positive) => hir::TraitBoundModifier::None,
|
2023-12-18 17:55:55 +01:00
|
|
|
(_, BoundPolarity::Maybe(_)) => hir::TraitBoundModifier::Maybe,
|
2023-12-20 15:22:06 +01:00
|
|
|
(BoundConstness::Never, BoundPolarity::Negative(_)) => {
|
2023-04-25 05:15:50 +00:00
|
|
|
if self.tcx.features().negative_bounds {
|
|
|
|
hir::TraitBoundModifier::Negative
|
|
|
|
} else {
|
|
|
|
hir::TraitBoundModifier::None
|
|
|
|
}
|
|
|
|
}
|
2023-12-18 17:55:55 +01:00
|
|
|
(BoundConstness::Always(_), _) => hir::TraitBoundModifier::Const,
|
|
|
|
(BoundConstness::Maybe(_), _) => hir::TraitBoundModifier::MaybeConst,
|
2016-05-10 01:11:59 +00:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2015-09-28 15:00:15 +13:00
|
|
|
|
2016-05-10 01:15:11 +00:00
|
|
|
// Helper methods for building HIR.
|
2015-09-28 15:00:15 +13:00
|
|
|
|
2019-11-29 13:43:03 +01:00
|
|
|
fn stmt(&mut self, span: Span, kind: hir::StmtKind<'hir>) -> hir::Stmt<'hir> {
|
2021-08-21 00:29:08 +03:00
|
|
|
hir::Stmt { span: self.lower_span(span), kind, hir_id: self.next_id() }
|
2019-03-11 16:43:27 +01:00
|
|
|
}
|
|
|
|
|
2019-11-29 13:43:03 +01:00
|
|
|
fn stmt_expr(&mut self, span: Span, expr: hir::Expr<'hir>) -> hir::Stmt<'hir> {
|
2019-11-29 19:01:31 +01:00
|
|
|
self.stmt(span, hir::StmtKind::Expr(self.arena.alloc(expr)))
|
2019-08-10 12:38:19 +02:00
|
|
|
}
|
|
|
|
|
2018-03-21 04:24:27 -04:00
|
|
|
fn stmt_let_pat(
|
|
|
|
&mut self,
|
2021-01-24 17:14:17 +01:00
|
|
|
attrs: Option<&'hir [Attribute]>,
|
2019-03-11 16:43:27 +01:00
|
|
|
span: Span,
|
2019-11-29 19:01:31 +01:00
|
|
|
init: Option<&'hir hir::Expr<'hir>>,
|
|
|
|
pat: &'hir hir::Pat<'hir>,
|
2018-03-21 04:24:27 -04:00
|
|
|
source: hir::LocalSource,
|
2019-11-29 13:43:03 +01:00
|
|
|
) -> hir::Stmt<'hir> {
|
2020-11-24 18:12:42 +01:00
|
|
|
let hir_id = self.next_id();
|
2021-01-24 17:14:17 +01:00
|
|
|
if let Some(a) = attrs {
|
|
|
|
debug_assert!(!a.is_empty());
|
2021-07-16 14:42:26 +02:00
|
|
|
self.attrs.insert(hir_id.local_id, a);
|
2021-01-24 17:14:17 +01:00
|
|
|
}
|
2024-03-20 17:50:31 +01:00
|
|
|
let local = hir::LetStmt {
|
2022-07-05 23:31:18 +02:00
|
|
|
hir_id,
|
|
|
|
init,
|
|
|
|
pat,
|
|
|
|
els: None,
|
|
|
|
source,
|
|
|
|
span: self.lower_span(span),
|
|
|
|
ty: None,
|
|
|
|
};
|
2024-03-14 11:53:38 +01:00
|
|
|
self.stmt(span, hir::StmtKind::Let(self.arena.alloc(local)))
|
2019-03-11 16:43:27 +01:00
|
|
|
}
|
2019-02-02 15:40:08 +01:00
|
|
|
|
2019-12-01 21:10:43 +01:00
|
|
|
fn block_expr(&mut self, expr: &'hir hir::Expr<'hir>) -> &'hir hir::Block<'hir> {
|
2019-11-29 19:01:31 +01:00
|
|
|
self.block_all(expr.span, &[], Some(expr))
|
2016-05-10 01:15:11 +00:00
|
|
|
}
|
2015-09-28 15:00:15 +13:00
|
|
|
|
2018-03-21 04:24:27 -04:00
|
|
|
fn block_all(
|
|
|
|
&mut self,
|
|
|
|
span: Span,
|
2019-11-29 19:01:31 +01:00
|
|
|
stmts: &'hir [hir::Stmt<'hir>],
|
|
|
|
expr: Option<&'hir hir::Expr<'hir>>,
|
2019-12-01 21:10:43 +01:00
|
|
|
) -> &'hir hir::Block<'hir> {
|
|
|
|
let blk = hir::Block {
|
2017-07-03 11:19:51 -07:00
|
|
|
stmts,
|
|
|
|
expr,
|
2019-04-26 14:23:30 +02:00
|
|
|
hir_id: self.next_id(),
|
2020-01-05 01:50:05 +01:00
|
|
|
rules: hir::BlockCheckMode::DefaultBlock,
|
2021-08-21 00:29:08 +03:00
|
|
|
span: self.lower_span(span),
|
2017-03-22 11:40:29 -04:00
|
|
|
targeted_by_break: false,
|
2019-12-01 21:10:43 +01:00
|
|
|
};
|
|
|
|
self.arena.alloc(blk)
|
2016-05-10 01:15:11 +00:00
|
|
|
}
|
2015-09-28 15:00:15 +13:00
|
|
|
|
2021-04-15 00:33:55 -07:00
|
|
|
fn pat_cf_continue(&mut self, span: Span, pat: &'hir hir::Pat<'hir>) -> &'hir hir::Pat<'hir> {
|
2020-08-04 14:34:24 +01:00
|
|
|
let field = self.single_pat_field(span, pat);
|
2023-11-23 06:01:35 +00:00
|
|
|
self.pat_lang_item_variant(span, hir::LangItem::ControlFlowContinue, field)
|
2016-05-10 01:15:11 +00:00
|
|
|
}
|
2015-09-28 15:00:15 +13:00
|
|
|
|
2021-04-15 00:33:55 -07:00
|
|
|
fn pat_cf_break(&mut self, span: Span, pat: &'hir hir::Pat<'hir>) -> &'hir hir::Pat<'hir> {
|
2020-08-04 14:34:24 +01:00
|
|
|
let field = self.single_pat_field(span, pat);
|
2023-11-23 06:01:35 +00:00
|
|
|
self.pat_lang_item_variant(span, hir::LangItem::ControlFlowBreak, field)
|
2016-05-10 01:15:11 +00:00
|
|
|
}
|
2016-02-28 17:38:48 -05:00
|
|
|
|
2019-11-29 19:01:31 +01:00
|
|
|
fn pat_some(&mut self, span: Span, pat: &'hir hir::Pat<'hir>) -> &'hir hir::Pat<'hir> {
|
2020-08-04 14:34:24 +01:00
|
|
|
let field = self.single_pat_field(span, pat);
|
2023-11-23 06:01:35 +00:00
|
|
|
self.pat_lang_item_variant(span, hir::LangItem::OptionSome, field)
|
2016-05-10 01:15:11 +00:00
|
|
|
}
|
2016-02-28 17:38:48 -05:00
|
|
|
|
2019-11-29 19:01:31 +01:00
|
|
|
fn pat_none(&mut self, span: Span) -> &'hir hir::Pat<'hir> {
|
2023-11-23 06:01:35 +00:00
|
|
|
self.pat_lang_item_variant(span, hir::LangItem::OptionNone, &[])
|
2016-05-10 01:15:11 +00:00
|
|
|
}
|
2015-09-28 15:00:15 +13:00
|
|
|
|
2020-08-04 14:34:24 +01:00
|
|
|
fn single_pat_field(
|
2018-03-21 04:24:27 -04:00
|
|
|
&mut self,
|
|
|
|
span: Span,
|
2020-08-04 14:34:24 +01:00
|
|
|
pat: &'hir hir::Pat<'hir>,
|
2021-03-16 00:36:07 +03:00
|
|
|
) -> &'hir [hir::PatField<'hir>] {
|
|
|
|
let field = hir::PatField {
|
2020-08-04 14:34:24 +01:00
|
|
|
hir_id: self.next_id(),
|
2021-08-21 00:29:08 +03:00
|
|
|
ident: Ident::new(sym::integer(0), self.lower_span(span)),
|
2020-08-04 14:34:24 +01:00
|
|
|
is_shorthand: false,
|
|
|
|
pat,
|
2021-08-21 00:29:08 +03:00
|
|
|
span: self.lower_span(span),
|
2016-05-10 01:15:11 +00:00
|
|
|
};
|
2020-08-04 14:34:24 +01:00
|
|
|
arena_vec![self; field]
|
|
|
|
}
|
|
|
|
|
|
|
|
fn pat_lang_item_variant(
|
|
|
|
&mut self,
|
|
|
|
span: Span,
|
|
|
|
lang_item: hir::LangItem,
|
2021-03-16 00:36:07 +03:00
|
|
|
fields: &'hir [hir::PatField<'hir>],
|
2020-08-04 14:34:24 +01:00
|
|
|
) -> &'hir hir::Pat<'hir> {
|
2023-11-23 06:01:35 +00:00
|
|
|
let qpath = hir::QPath::LangItem(lang_item, self.lower_span(span));
|
2020-08-04 14:34:24 +01:00
|
|
|
self.pat(span, hir::PatKind::Struct(qpath, fields, false))
|
2016-05-10 01:15:11 +00:00
|
|
|
}
|
2015-09-28 15:00:15 +13:00
|
|
|
|
2024-03-06 17:24:13 +11:00
|
|
|
fn pat_ident(&mut self, span: Span, ident: Ident) -> (&'hir hir::Pat<'hir>, HirId) {
|
2024-04-16 19:23:30 -04:00
|
|
|
self.pat_ident_binding_mode(span, ident, hir::BindingMode::NONE)
|
2016-05-10 01:15:11 +00:00
|
|
|
}
|
2015-09-28 15:00:15 +13:00
|
|
|
|
2024-03-06 17:24:13 +11:00
|
|
|
fn pat_ident_mut(&mut self, span: Span, ident: Ident) -> (hir::Pat<'hir>, HirId) {
|
2024-04-16 19:23:30 -04:00
|
|
|
self.pat_ident_binding_mode_mut(span, ident, hir::BindingMode::NONE)
|
2021-07-14 16:17:04 -05:00
|
|
|
}
|
|
|
|
|
2018-03-21 04:24:27 -04:00
|
|
|
fn pat_ident_binding_mode(
|
|
|
|
&mut self,
|
|
|
|
span: Span,
|
2018-06-10 17:40:45 +03:00
|
|
|
ident: Ident,
|
2024-04-16 19:23:30 -04:00
|
|
|
bm: hir::BindingMode,
|
2024-03-06 17:24:13 +11:00
|
|
|
) -> (&'hir hir::Pat<'hir>, HirId) {
|
2021-07-14 16:17:04 -05:00
|
|
|
let (pat, hir_id) = self.pat_ident_binding_mode_mut(span, ident, bm);
|
|
|
|
(self.arena.alloc(pat), hir_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn pat_ident_binding_mode_mut(
|
|
|
|
&mut self,
|
|
|
|
span: Span,
|
|
|
|
ident: Ident,
|
2024-04-16 19:23:30 -04:00
|
|
|
bm: hir::BindingMode,
|
2024-03-06 17:24:13 +11:00
|
|
|
) -> (hir::Pat<'hir>, HirId) {
|
2019-04-26 14:23:30 +02:00
|
|
|
let hir_id = self.next_id();
|
2015-09-28 17:24:42 +13:00
|
|
|
|
2019-03-01 09:31:58 +01:00
|
|
|
(
|
2021-07-14 16:17:04 -05:00
|
|
|
hir::Pat {
|
2019-03-01 09:31:58 +01:00
|
|
|
hir_id,
|
2021-08-21 00:29:08 +03:00
|
|
|
kind: hir::PatKind::Binding(bm, hir_id, self.lower_ident(ident), None),
|
|
|
|
span: self.lower_span(span),
|
2020-11-04 16:32:52 +00:00
|
|
|
default_binding_modes: true,
|
2021-07-14 16:17:04 -05:00
|
|
|
},
|
2019-12-24 17:38:22 -05:00
|
|
|
hir_id,
|
2019-03-01 09:31:58 +01:00
|
|
|
)
|
2016-05-10 01:15:11 +00:00
|
|
|
}
|
2015-09-28 15:00:15 +13:00
|
|
|
|
2019-11-29 19:01:31 +01:00
|
|
|
fn pat(&mut self, span: Span, kind: hir::PatKind<'hir>) -> &'hir hir::Pat<'hir> {
|
2020-11-04 16:32:52 +00:00
|
|
|
self.arena.alloc(hir::Pat {
|
|
|
|
hir_id: self.next_id(),
|
|
|
|
kind,
|
2021-08-21 00:29:08 +03:00
|
|
|
span: self.lower_span(span),
|
2020-11-04 16:32:52 +00:00
|
|
|
default_binding_modes: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-07-14 16:17:04 -05:00
|
|
|
fn pat_without_dbm(&mut self, span: Span, kind: hir::PatKind<'hir>) -> hir::Pat<'hir> {
|
2021-08-21 00:29:08 +03:00
|
|
|
hir::Pat {
|
|
|
|
hir_id: self.next_id(),
|
|
|
|
kind,
|
|
|
|
span: self.lower_span(span),
|
|
|
|
default_binding_modes: false,
|
|
|
|
}
|
2016-05-10 01:15:11 +00:00
|
|
|
}
|
2015-09-28 15:00:15 +13:00
|
|
|
|
2024-03-06 17:24:13 +11:00
|
|
|
fn ty_path(&mut self, mut hir_id: HirId, span: Span, qpath: hir::QPath<'hir>) -> hir::Ty<'hir> {
|
2019-09-26 17:25:31 +01:00
|
|
|
let kind = match qpath {
|
2017-01-17 16:46:23 +02:00
|
|
|
hir::QPath::Resolved(None, path) => {
|
2018-07-11 22:41:03 +08:00
|
|
|
// Turn trait object paths into `TyKind::TraitObject` instead.
|
2019-04-20 19:36:05 +03:00
|
|
|
match path.res {
|
2020-04-16 17:38:52 -07:00
|
|
|
Res::Def(DefKind::Trait | DefKind::TraitAlias, _) => {
|
2018-10-22 00:45:24 +01:00
|
|
|
let principal = hir::PolyTraitRef {
|
2019-12-01 11:22:58 +01:00
|
|
|
bound_generic_params: &[],
|
2019-12-24 17:38:22 -05:00
|
|
|
trait_ref: hir::TraitRef { path, hir_ref_id: hir_id },
|
2021-08-21 00:29:08 +03:00
|
|
|
span: self.lower_span(span),
|
2018-10-22 00:45:24 +01:00
|
|
|
};
|
2017-01-17 16:46:23 +02:00
|
|
|
|
2018-10-22 00:45:24 +01:00
|
|
|
// The original ID is taken by the `PolyTraitRef`,
|
|
|
|
// so the `Ty` itself needs a different one.
|
2019-04-26 14:23:30 +02:00
|
|
|
hir_id = self.next_id();
|
2019-12-01 11:22:58 +01:00
|
|
|
hir::TyKind::TraitObject(
|
|
|
|
arena_vec![self; principal],
|
|
|
|
self.elided_dyn_bound(span),
|
2021-03-13 15:44:29 +03:00
|
|
|
TraitObjectSyntax::None,
|
2019-12-01 11:22:58 +01:00
|
|
|
)
|
2018-10-22 00:45:24 +01:00
|
|
|
}
|
|
|
|
_ => hir::TyKind::Path(hir::QPath::Resolved(None, path)),
|
2017-01-17 16:46:23 +02:00
|
|
|
}
|
|
|
|
}
|
2018-07-11 22:41:03 +08:00
|
|
|
_ => hir::TyKind::Path(qpath),
|
2017-01-17 16:46:23 +02:00
|
|
|
};
|
2019-09-26 17:25:31 +01:00
|
|
|
|
2021-08-21 00:29:08 +03:00
|
|
|
hir::Ty { hir_id, kind, span: self.lower_span(span) }
|
2016-10-27 05:17:42 +03:00
|
|
|
}
|
2017-01-09 17:46:11 +02:00
|
|
|
|
2018-03-21 16:59:28 -04:00
|
|
|
/// Invoked to create the lifetime argument(s) for an elided trait object
|
|
|
|
/// bound, like the bound in `Box<dyn Debug>`. This method is not invoked
|
|
|
|
/// when the bound is written, even if it is written with `'_` like in
|
|
|
|
/// `Box<dyn Debug + '_>`. In those cases, `lower_lifetime` is invoked.
|
2022-09-01 12:06:48 +10:00
|
|
|
fn elided_dyn_bound(&mut self, span: Span) -> &'hir hir::Lifetime {
|
2019-08-08 03:36:24 -04:00
|
|
|
let r = hir::Lifetime {
|
|
|
|
hir_id: self.next_id(),
|
2022-11-05 22:41:07 +00:00
|
|
|
ident: Ident::new(kw::Empty, self.lower_span(span)),
|
|
|
|
res: hir::LifetimeName::ImplicitObjectLifetimeDefault,
|
2019-08-08 03:36:24 -04:00
|
|
|
};
|
|
|
|
debug!("elided_dyn_bound: r={:?}", r);
|
2022-09-01 12:06:48 +10:00
|
|
|
self.arena.alloc(r)
|
2018-03-21 16:59:28 -04:00
|
|
|
}
|
2015-09-29 13:17:46 +13:00
|
|
|
}
|
2017-02-21 12:23:47 -05:00
|
|
|
|
2019-12-01 17:10:12 +01:00
|
|
|
/// Helper struct for delayed construction of GenericArgs.
|
|
|
|
struct GenericArgsCtor<'hir> {
|
2019-12-30 23:25:47 +01:00
|
|
|
args: SmallVec<[hir::GenericArg<'hir>; 4]>,
|
2019-12-01 17:10:12 +01:00
|
|
|
bindings: &'hir [hir::TypeBinding<'hir>],
|
2023-03-16 22:00:08 +00:00
|
|
|
parenthesized: hir::GenericArgsParentheses,
|
2021-05-12 11:36:07 +02:00
|
|
|
span: Span,
|
2019-12-01 17:10:12 +01:00
|
|
|
}
|
|
|
|
|
2019-12-22 18:15:02 +01:00
|
|
|
impl<'hir> GenericArgsCtor<'hir> {
|
2023-12-18 17:55:55 +01:00
|
|
|
fn push_constness(
|
|
|
|
&mut self,
|
|
|
|
lcx: &mut LoweringContext<'_, 'hir>,
|
|
|
|
constness: ast::BoundConstness,
|
|
|
|
) {
|
2023-07-25 05:58:53 +00:00
|
|
|
if !lcx.tcx.features().effects {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-12-18 17:55:55 +01:00
|
|
|
let (span, body) = match constness {
|
|
|
|
BoundConstness::Never => return,
|
|
|
|
BoundConstness::Always(span) => {
|
|
|
|
let span = lcx.lower_span(span);
|
2023-07-25 05:58:53 +00:00
|
|
|
|
2023-12-18 17:55:55 +01:00
|
|
|
let body = hir::ExprKind::Lit(
|
|
|
|
lcx.arena.alloc(hir::Lit { node: LitKind::Bool(false), span }),
|
|
|
|
);
|
2023-07-25 05:58:53 +00:00
|
|
|
|
2023-12-18 17:55:55 +01:00
|
|
|
(span, body)
|
|
|
|
}
|
|
|
|
BoundConstness::Maybe(span) => {
|
|
|
|
let span = lcx.lower_span(span);
|
2023-08-13 13:59:19 +00:00
|
|
|
|
2023-12-18 17:55:55 +01:00
|
|
|
let Some(host_param_id) = lcx.host_param_id else {
|
|
|
|
lcx.dcx().span_delayed_bug(
|
|
|
|
span,
|
|
|
|
"no host param id for call in const yet no errors reported",
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
};
|
2023-08-13 13:59:19 +00:00
|
|
|
|
|
|
|
let hir_id = lcx.next_id();
|
|
|
|
let res = Res::Def(DefKind::ConstParam, host_param_id.to_def_id());
|
2023-12-18 17:55:55 +01:00
|
|
|
let body = hir::ExprKind::Path(hir::QPath::Resolved(
|
2023-08-13 13:59:19 +00:00
|
|
|
None,
|
|
|
|
lcx.arena.alloc(hir::Path {
|
2023-07-25 05:58:53 +00:00
|
|
|
span,
|
2023-08-13 13:59:19 +00:00
|
|
|
res,
|
2023-12-18 17:55:55 +01:00
|
|
|
segments: arena_vec![
|
|
|
|
lcx;
|
|
|
|
hir::PathSegment::new(
|
|
|
|
Ident { name: sym::host, span },
|
|
|
|
hir_id,
|
|
|
|
res
|
|
|
|
)
|
|
|
|
],
|
2023-08-13 13:59:19 +00:00
|
|
|
}),
|
|
|
|
));
|
2023-12-18 17:55:55 +01:00
|
|
|
|
|
|
|
(span, body)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let body = lcx.lower_body(|lcx| (&[], lcx.expr(span, body)));
|
|
|
|
|
|
|
|
let id = lcx.next_node_id();
|
|
|
|
let hir_id = lcx.next_id();
|
2023-08-06 17:00:26 +00:00
|
|
|
|
2023-11-21 23:40:23 +03:00
|
|
|
let def_id = lcx.create_def(
|
|
|
|
lcx.current_hir_id_owner.def_id,
|
|
|
|
id,
|
2023-12-03 12:29:59 +03:00
|
|
|
kw::Empty,
|
2023-11-21 23:40:23 +03:00
|
|
|
DefKind::AnonConst,
|
|
|
|
span,
|
|
|
|
);
|
2023-12-04 14:22:05 +00:00
|
|
|
|
2023-07-25 05:58:53 +00:00
|
|
|
lcx.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id)));
|
|
|
|
self.args.push(hir::GenericArg::Const(hir::ConstArg {
|
|
|
|
value: hir::AnonConst { def_id, hir_id, body },
|
|
|
|
span,
|
2023-10-25 15:28:23 +00:00
|
|
|
is_desugared_from_effects: true,
|
2023-07-25 05:58:53 +00:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2019-12-01 17:10:12 +01:00
|
|
|
fn is_empty(&self) -> bool {
|
2023-03-16 22:00:08 +00:00
|
|
|
self.args.is_empty()
|
|
|
|
&& self.bindings.is_empty()
|
|
|
|
&& self.parenthesized == hir::GenericArgsParentheses::No
|
2019-12-01 17:10:12 +01:00
|
|
|
}
|
|
|
|
|
2021-08-21 00:29:08 +03:00
|
|
|
fn into_generic_args(self, this: &LoweringContext<'_, 'hir>) -> &'hir hir::GenericArgs<'hir> {
|
|
|
|
let ga = hir::GenericArgs {
|
|
|
|
args: this.arena.alloc_from_iter(self.args),
|
2019-12-01 17:10:12 +01:00
|
|
|
bindings: self.bindings,
|
|
|
|
parenthesized: self.parenthesized,
|
2021-08-21 00:29:08 +03:00
|
|
|
span_ext: this.lower_span(self.span),
|
|
|
|
};
|
|
|
|
this.arena.alloc(ga)
|
2019-12-01 17:10:12 +01:00
|
|
|
}
|
|
|
|
}
|