summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_ast_lowering/src
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-18 02:49:50 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-18 02:49:50 +0000
commit9835e2ae736235810b4ea1c162ca5e65c547e770 (patch)
tree3fcebf40ed70e581d776a8a4c65923e8ec20e026 /compiler/rustc_ast_lowering/src
parentReleasing progress-linux version 1.70.0+dfsg2-1~progress7.99u1. (diff)
downloadrustc-9835e2ae736235810b4ea1c162ca5e65c547e770.tar.xz
rustc-9835e2ae736235810b4ea1c162ca5e65c547e770.zip
Merging upstream version 1.71.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'compiler/rustc_ast_lowering/src')
-rw-r--r--compiler/rustc_ast_lowering/src/errors.rs2
-rw-r--r--compiler/rustc_ast_lowering/src/expr.rs93
-rw-r--r--compiler/rustc_ast_lowering/src/format.rs59
-rw-r--r--compiler/rustc_ast_lowering/src/index.rs2
-rw-r--r--compiler/rustc_ast_lowering/src/item.rs36
-rw-r--r--compiler/rustc_ast_lowering/src/lib.rs108
-rw-r--r--compiler/rustc_ast_lowering/src/path.rs2
7 files changed, 170 insertions, 132 deletions
diff --git a/compiler/rustc_ast_lowering/src/errors.rs b/compiler/rustc_ast_lowering/src/errors.rs
index 3e9f9b436..72dc52a63 100644
--- a/compiler/rustc_ast_lowering/src/errors.rs
+++ b/compiler/rustc_ast_lowering/src/errors.rs
@@ -108,7 +108,7 @@ pub struct BaseExpressionDoubleDot {
pub struct AwaitOnlyInAsyncFnAndBlocks {
#[primary_span]
#[label]
- pub dot_await_span: Span,
+ pub await_kw_span: Span,
#[label(ast_lowering_this_not_async)]
pub item_span: Option<Span>,
}
diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs
index 1b1c4765b..5e0ab80c6 100644
--- a/compiler/rustc_ast_lowering/src/expr.rs
+++ b/compiler/rustc_ast_lowering/src/expr.rs
@@ -121,12 +121,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
LitKind::Err
}
};
- hir::ExprKind::Lit(respan(self.lower_span(e.span), lit_kind))
+ let lit = self.arena.alloc(respan(self.lower_span(e.span), lit_kind));
+ hir::ExprKind::Lit(lit)
+ }
+ ExprKind::IncludedBytes(bytes) => {
+ let lit = self.arena.alloc(respan(
+ self.lower_span(e.span),
+ LitKind::ByteStr(bytes.clone(), StrStyle::Cooked),
+ ));
+ hir::ExprKind::Lit(lit)
}
- ExprKind::IncludedBytes(bytes) => hir::ExprKind::Lit(respan(
- self.lower_span(e.span),
- LitKind::ByteStr(bytes.clone(), StrStyle::Cooked),
- )),
ExprKind::Cast(expr, ty) => {
let expr = self.lower_expr(expr);
let ty =
@@ -181,21 +185,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
hir::AsyncGeneratorKind::Block,
|this| this.with_new_scopes(|this| this.lower_block_expr(block)),
),
- ExprKind::Await(expr) => {
- let dot_await_span = if expr.span.hi() < e.span.hi() {
- let span_with_whitespace = self
- .tcx
- .sess
- .source_map()
- .span_extend_while(expr.span, char::is_whitespace)
- .unwrap_or(expr.span);
- span_with_whitespace.shrink_to_hi().with_hi(e.span.hi())
- } else {
- // this is a recovered `await expr`
- e.span
- };
- self.lower_expr_await(dot_await_span, expr)
- }
+ ExprKind::Await(expr, await_kw_span) => self.lower_expr_await(*await_kw_span, expr),
ExprKind::Closure(box Closure {
binder,
capture_clause,
@@ -285,6 +275,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
hir::ExprKind::InlineAsm(self.lower_inline_asm(e.span, asm))
}
ExprKind::FormatArgs(fmt) => self.lower_format_args(e.span, fmt),
+ ExprKind::OffsetOf(container, fields) => hir::ExprKind::OffsetOf(
+ self.lower_ty(
+ container,
+ &mut ImplTraitContext::Disallowed(ImplTraitPosition::OffsetOf),
+ ),
+ self.arena.alloc_from_iter(fields.iter().map(|&ident| self.lower_ident(ident))),
+ ),
ExprKind::Struct(se) => {
let rest = match &se.rest {
StructRest::Base(e) => Some(self.lower_expr(e)),
@@ -699,18 +696,18 @@ impl<'hir> LoweringContext<'_, 'hir> {
/// }
/// }
/// ```
- fn lower_expr_await(&mut self, dot_await_span: Span, expr: &Expr) -> hir::ExprKind<'hir> {
- let full_span = expr.span.to(dot_await_span);
+ fn lower_expr_await(&mut self, await_kw_span: Span, expr: &Expr) -> hir::ExprKind<'hir> {
+ let full_span = expr.span.to(await_kw_span);
match self.generator_kind {
Some(hir::GeneratorKind::Async(_)) => {}
Some(hir::GeneratorKind::Gen) | None => {
self.tcx.sess.emit_err(AwaitOnlyInAsyncFnAndBlocks {
- dot_await_span,
+ await_kw_span,
item_span: self.current_item,
});
}
}
- let span = self.mark_span_with_reason(DesugaringKind::Await, dot_await_span, None);
+ let span = self.mark_span_with_reason(DesugaringKind::Await, await_kw_span, None);
let gen_future_span = self.mark_span_with_reason(
DesugaringKind::Await,
full_span,
@@ -847,13 +844,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
let awaitee_arm = self.arm(awaitee_pat, loop_expr);
// `match ::std::future::IntoFuture::into_future(<expr>) { ... }`
- let into_future_span = self.mark_span_with_reason(
- DesugaringKind::Await,
- dot_await_span,
- self.allow_into_future.clone(),
- );
let into_future_expr = self.expr_call_lang_item_fn(
- into_future_span,
+ span,
hir::LangItem::IntoFutureIntoFuture,
arena_vec![self; expr],
Some(expr_hir_id),
@@ -1746,40 +1738,31 @@ impl<'hir> LoweringContext<'_, 'hir> {
}
pub(super) fn expr_usize(&mut self, sp: Span, value: usize) -> hir::Expr<'hir> {
- self.expr(
- sp,
- hir::ExprKind::Lit(hir::Lit {
- span: sp,
- node: ast::LitKind::Int(
- value as u128,
- ast::LitIntType::Unsigned(ast::UintTy::Usize),
- ),
- }),
- )
+ let lit = self.arena.alloc(hir::Lit {
+ span: sp,
+ node: ast::LitKind::Int(value as u128, ast::LitIntType::Unsigned(ast::UintTy::Usize)),
+ });
+ self.expr(sp, hir::ExprKind::Lit(lit))
}
pub(super) fn expr_u32(&mut self, sp: Span, value: u32) -> hir::Expr<'hir> {
- self.expr(
- sp,
- hir::ExprKind::Lit(hir::Lit {
- span: sp,
- node: ast::LitKind::Int(value.into(), ast::LitIntType::Unsigned(ast::UintTy::U32)),
- }),
- )
+ let lit = self.arena.alloc(hir::Lit {
+ span: sp,
+ node: ast::LitKind::Int(value.into(), ast::LitIntType::Unsigned(ast::UintTy::U32)),
+ });
+ self.expr(sp, hir::ExprKind::Lit(lit))
}
pub(super) fn expr_char(&mut self, sp: Span, value: char) -> hir::Expr<'hir> {
- self.expr(sp, hir::ExprKind::Lit(hir::Lit { span: sp, node: ast::LitKind::Char(value) }))
+ let lit = self.arena.alloc(hir::Lit { span: sp, node: ast::LitKind::Char(value) });
+ self.expr(sp, hir::ExprKind::Lit(lit))
}
pub(super) fn expr_str(&mut self, sp: Span, value: Symbol) -> hir::Expr<'hir> {
- self.expr(
- sp,
- hir::ExprKind::Lit(hir::Lit {
- span: sp,
- node: ast::LitKind::Str(value, ast::StrStyle::Cooked),
- }),
- )
+ let lit = self
+ .arena
+ .alloc(hir::Lit { span: sp, node: ast::LitKind::Str(value, ast::StrStyle::Cooked) });
+ self.expr(sp, hir::ExprKind::Lit(lit))
}
pub(super) fn expr_call_mut(
diff --git a/compiler/rustc_ast_lowering/src/format.rs b/compiler/rustc_ast_lowering/src/format.rs
index c41bdc440..afcf8b15c 100644
--- a/compiler/rustc_ast_lowering/src/format.rs
+++ b/compiler/rustc_ast_lowering/src/format.rs
@@ -186,7 +186,7 @@ enum ArgumentType {
/// Generates:
///
/// ```text
-/// <core::fmt::ArgumentV1>::new_…(arg)
+/// <core::fmt::Argument>::new_…(arg)
/// ```
fn make_argument<'hir>(
ctx: &mut LoweringContext<'_, 'hir>,
@@ -220,19 +220,19 @@ fn make_argument<'hir>(
/// Generates:
///
/// ```text
-/// <core::fmt::rt::v1::Count>::Is(…)
+/// <core::fmt::rt::Count>::Is(…)
/// ```
///
/// or
///
/// ```text
-/// <core::fmt::rt::v1::Count>::Param(…)
+/// <core::fmt::rt::Count>::Param(…)
/// ```
///
/// or
///
/// ```text
-/// <core::fmt::rt::v1::Count>::Implied
+/// <core::fmt::rt::Count>::Implied
/// ```
fn make_count<'hir>(
ctx: &mut LoweringContext<'_, 'hir>,
@@ -278,13 +278,13 @@ fn make_count<'hir>(
/// Generates
///
/// ```text
-/// <core::fmt::rt::v1::Argument::new(
+/// <core::fmt::rt::Placeholder::new(
/// …usize, // position
/// '…', // fill
-/// <core::fmt::rt::v1::Alignment>::…, // alignment
+/// <core::fmt::rt::Alignment>::…, // alignment
/// …u32, // flags
-/// <core::fmt::rt::v1::Count::…>, // width
-/// <core::fmt::rt::v1::Count::…>, // precision
+/// <core::fmt::rt::Count::…>, // width
+/// <core::fmt::rt::Count::…>, // precision
/// )
/// ```
fn make_format_spec<'hir>(
@@ -327,7 +327,7 @@ fn make_format_spec<'hir>(
None => sym::Unknown,
},
);
- // This needs to match `FlagV1` in library/core/src/fmt/mod.rs.
+ // This needs to match `Flag` in library/core/src/fmt/rt.rs.
let flags: u32 = ((sign == Some(FormatSign::Plus)) as u32)
| ((sign == Some(FormatSign::Minus)) as u32) << 1
| (alternate as u32) << 2
@@ -438,7 +438,7 @@ fn expand_format_args<'hir>(
// If the args array contains exactly all the original arguments once,
// in order, we can use a simple array instead of a `match` construction.
// However, if there's a yield point in any argument except the first one,
- // we don't do this, because an ArgumentV1 cannot be kept across yield points.
+ // we don't do this, because an Argument cannot be kept across yield points.
//
// This is an optimization, speeding up compilation about 1-2% in some cases.
// See https://github.com/rust-lang/rust/pull/106770#issuecomment-1380790609
@@ -446,12 +446,35 @@ fn expand_format_args<'hir>(
&& argmap.iter().enumerate().all(|(i, (&(j, _), _))| i == j)
&& arguments.iter().skip(1).all(|arg| !may_contain_yield_point(&arg.expr));
- let args = if use_simple_array {
+ let args = if arguments.is_empty() {
+ // Generate:
+ // &<core::fmt::Argument>::none()
+ //
+ // Note:
+ // `none()` just returns `[]`. We use `none()` rather than `[]` to limit the lifetime.
+ //
+ // This makes sure that this still fails to compile, even when the argument is inlined:
+ //
+ // ```
+ // let f = format_args!("{}", "a");
+ // println!("{f}"); // error E0716
+ // ```
+ //
+ // Cases where keeping the object around is allowed, such as `format_args!("a")`,
+ // are handled above by the `allow_const` case.
+ let none_fn = ctx.arena.alloc(ctx.expr_lang_item_type_relative(
+ macsp,
+ hir::LangItem::FormatArgument,
+ sym::none,
+ ));
+ let none = ctx.expr_call(macsp, none_fn, &[]);
+ ctx.expr(macsp, hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Not, none))
+ } else if use_simple_array {
// Generate:
// &[
- // <core::fmt::ArgumentV1>::new_display(&arg0),
- // <core::fmt::ArgumentV1>::new_lower_hex(&arg1),
- // <core::fmt::ArgumentV1>::new_debug(&arg2),
+ // <core::fmt::Argument>::new_display(&arg0),
+ // <core::fmt::Argument>::new_lower_hex(&arg1),
+ // <core::fmt::Argument>::new_debug(&arg2),
// …
// ]
let elements: Vec<_> = arguments
@@ -477,9 +500,9 @@ fn expand_format_args<'hir>(
// Generate:
// &match (&arg0, &arg1, &…) {
// args => [
- // <core::fmt::ArgumentV1>::new_display(args.0),
- // <core::fmt::ArgumentV1>::new_lower_hex(args.1),
- // <core::fmt::ArgumentV1>::new_debug(args.0),
+ // <core::fmt::Argument>::new_display(args.0),
+ // <core::fmt::Argument>::new_lower_hex(args.1),
+ // <core::fmt::Argument>::new_debug(args.0),
// …
// ]
// }
@@ -583,7 +606,7 @@ fn may_contain_yield_point(e: &ast::Expr) -> bool {
impl Visitor<'_> for MayContainYieldPoint {
fn visit_expr(&mut self, e: &ast::Expr) {
- if let ast::ExprKind::Await(_) | ast::ExprKind::Yield(_) = e.kind {
+ if let ast::ExprKind::Await(_, _) | ast::ExprKind::Yield(_) = e.kind {
self.0 = true;
} else {
visit::walk_expr(self, e);
diff --git a/compiler/rustc_ast_lowering/src/index.rs b/compiler/rustc_ast_lowering/src/index.rs
index f7fe0d771..2e66c81eb 100644
--- a/compiler/rustc_ast_lowering/src/index.rs
+++ b/compiler/rustc_ast_lowering/src/index.rs
@@ -5,7 +5,7 @@ use rustc_hir::def_id::LocalDefId;
use rustc_hir::definitions;
use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::*;
-use rustc_index::vec::{Idx, IndexVec};
+use rustc_index::{Idx, IndexVec};
use rustc_middle::span_bug;
use rustc_session::Session;
use rustc_span::source_map::SourceMap;
diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs
index f89e254a2..08ee3761b 100644
--- a/compiler/rustc_ast_lowering/src/item.rs
+++ b/compiler/rustc_ast_lowering/src/item.rs
@@ -12,7 +12,7 @@ use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID};
use rustc_hir::PredicateOrigin;
-use rustc_index::vec::{Idx, IndexSlice, IndexVec};
+use rustc_index::{Idx, IndexSlice, IndexVec};
use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
use rustc_span::edit_distance::find_best_match_for_name;
use rustc_span::source_map::DesugaringKind;
@@ -83,15 +83,14 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
impl_trait_bounds: Vec::new(),
allow_try_trait: Some([sym::try_trait_v2, sym::yeet_desugar_details][..].into()),
allow_gen_future: Some([sym::gen_future, sym::closure_track_caller][..].into()),
- allow_into_future: Some([sym::into_future][..].into()),
generics_def_id_map: Default::default(),
};
lctx.with_hir_id_owner(owner, |lctx| f(lctx));
for (def_id, info) in lctx.children {
- self.owners.ensure_contains_elem(def_id, || hir::MaybeOwner::Phantom);
- debug_assert!(matches!(self.owners[def_id], hir::MaybeOwner::Phantom));
- self.owners[def_id] = info;
+ let owner = self.owners.ensure_contains_elem(def_id, || hir::MaybeOwner::Phantom);
+ debug_assert!(matches!(owner, hir::MaybeOwner::Phantom));
+ *owner = info;
}
}
@@ -99,8 +98,8 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
&mut self,
def_id: LocalDefId,
) -> hir::MaybeOwner<&'hir hir::OwnerInfo<'hir>> {
- self.owners.ensure_contains_elem(def_id, || hir::MaybeOwner::Phantom);
- if let hir::MaybeOwner::Phantom = self.owners[def_id] {
+ let owner = self.owners.ensure_contains_elem(def_id, || hir::MaybeOwner::Phantom);
+ if let hir::MaybeOwner::Phantom = owner {
let node = self.ast_index[def_id];
match node {
AstOwner::NonOwner => {}
@@ -138,12 +137,10 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
// Evaluate with the lifetimes in `params` in-scope.
// This is used to track which lifetimes have already been defined,
// and which need to be replicated when lowering an async fn.
- match parent_hir.node().expect_item().kind {
- hir::ItemKind::Impl(hir::Impl { of_trait, .. }) => {
- lctx.is_in_trait_impl = of_trait.is_some();
- }
- _ => {}
- };
+
+ if let hir::ItemKind::Impl(impl_) = parent_hir.node().expect_item().kind {
+ lctx.is_in_trait_impl = impl_.of_trait.is_some();
+ }
match ctxt {
AssocCtxt::Trait => hir::OwnerNode::TraitItem(lctx.lower_trait_item(item)),
@@ -308,7 +305,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
);
this.arena.alloc(this.ty(span, hir::TyKind::Err(guar)))
}
- Some(ty) => this.lower_ty(ty, &ImplTraitContext::TypeAliasesOpaqueTy),
+ Some(ty) => this.lower_ty(
+ ty,
+ &ImplTraitContext::TypeAliasesOpaqueTy { in_assoc_ty: false },
+ ),
},
);
hir::ItemKind::TyAlias(ty, generics)
@@ -445,7 +445,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
ItemKind::MacroDef(MacroDef { body, macro_rules }) => {
let body = P(self.lower_delim_args(body));
let macro_kind = self.resolver.decl_macro_kind(self.local_def_id(id));
- hir::ItemKind::Macro(ast::MacroDef { body, macro_rules: *macro_rules }, macro_kind)
+ let macro_def = self.arena.alloc(ast::MacroDef { body, macro_rules: *macro_rules });
+ hir::ItemKind::Macro(macro_def, macro_kind)
}
ItemKind::MacCall(..) => {
panic!("`TyMac` should have been expanded by now")
@@ -854,7 +855,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
hir::ImplItemKind::Type(ty)
}
Some(ty) => {
- let ty = this.lower_ty(ty, &ImplTraitContext::TypeAliasesOpaqueTy);
+ let ty = this.lower_ty(
+ ty,
+ &ImplTraitContext::TypeAliasesOpaqueTy { in_assoc_ty: true },
+ );
hir::ImplItemKind::Type(ty)
}
},
diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs
index f7ae96b7c..8d4f96639 100644
--- a/compiler/rustc_ast_lowering/src/lib.rs
+++ b/compiler/rustc_ast_lowering/src/lib.rs
@@ -55,13 +55,13 @@ use rustc_data_structures::sync::Lrc;
use rustc_errors::{
DiagnosticArgFromDisplay, DiagnosticMessage, Handler, StashKey, SubdiagnosticMessage,
};
+use rustc_fluent_macro::fluent_messages;
use rustc_hir as hir;
use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
-use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID};
+use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID, LOCAL_CRATE};
use rustc_hir::definitions::DefPathData;
use rustc_hir::{ConstArg, GenericArg, ItemLocalId, ParamName, TraitCandidate};
-use rustc_index::vec::{Idx, IndexSlice, IndexVec};
-use rustc_macros::fluent_messages;
+use rustc_index::{Idx, IndexSlice, IndexVec};
use rustc_middle::{
span_bug,
ty::{ResolverAstLowering, TyCtxt},
@@ -136,7 +136,6 @@ struct LoweringContext<'a, 'hir> {
allow_try_trait: Option<Lrc<[Symbol]>>,
allow_gen_future: Option<Lrc<[Symbol]>>,
- allow_into_future: Option<Lrc<[Symbol]>>,
/// 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
@@ -248,7 +247,7 @@ enum ImplTraitContext {
in_trait: bool,
},
/// Impl trait in type aliases.
- TypeAliasesOpaqueTy,
+ TypeAliasesOpaqueTy { in_assoc_ty: bool },
/// `impl Trait` is unstably accepted in this position.
FeatureGated(ImplTraitPosition, Symbol),
/// `impl Trait` is not accepted in this position.
@@ -283,6 +282,7 @@ enum ImplTraitPosition {
FieldTy,
Cast,
ImplSelf,
+ OffsetOf,
}
impl std::fmt::Display for ImplTraitPosition {
@@ -313,6 +313,7 @@ impl std::fmt::Display for ImplTraitPosition {
ImplTraitPosition::FieldTy => "field types",
ImplTraitPosition::Cast => "cast types",
ImplTraitPosition::ImplSelf => "impl headers",
+ ImplTraitPosition::OffsetOf => "`offset_of!` params",
};
write!(f, "{name}")
@@ -332,10 +333,7 @@ enum FnDeclKind {
impl FnDeclKind {
fn param_impl_trait_allowed(&self) -> bool {
- match self {
- FnDeclKind::Fn | FnDeclKind::Inherent | FnDeclKind::Impl | FnDeclKind::Trait => true,
- _ => false,
- }
+ matches!(self, FnDeclKind::Fn | FnDeclKind::Inherent | FnDeclKind::Impl | FnDeclKind::Trait)
}
fn return_impl_trait_allowed(&self, tcx: TyCtxt<'_>) -> bool {
@@ -371,8 +369,8 @@ fn index_crate<'a>(
krate: &'a Crate,
) -> IndexVec<LocalDefId, AstOwner<'a>> {
let mut indexer = Indexer { node_id_to_def_id, index: IndexVec::new() };
- indexer.index.ensure_contains_elem(CRATE_DEF_ID, || AstOwner::NonOwner);
- indexer.index[CRATE_DEF_ID] = AstOwner::Crate(krate);
+ *indexer.index.ensure_contains_elem(CRATE_DEF_ID, || AstOwner::NonOwner) =
+ AstOwner::Crate(krate);
visit::walk_crate(&mut indexer, krate);
return indexer.index;
@@ -389,22 +387,21 @@ fn index_crate<'a>(
fn visit_item(&mut self, item: &'a ast::Item) {
let def_id = self.node_id_to_def_id[&item.id];
- self.index.ensure_contains_elem(def_id, || AstOwner::NonOwner);
- self.index[def_id] = AstOwner::Item(item);
+ *self.index.ensure_contains_elem(def_id, || AstOwner::NonOwner) = AstOwner::Item(item);
visit::walk_item(self, item)
}
fn visit_assoc_item(&mut self, item: &'a ast::AssocItem, ctxt: visit::AssocCtxt) {
let def_id = self.node_id_to_def_id[&item.id];
- self.index.ensure_contains_elem(def_id, || AstOwner::NonOwner);
- self.index[def_id] = AstOwner::AssocItem(item, ctxt);
+ *self.index.ensure_contains_elem(def_id, || AstOwner::NonOwner) =
+ AstOwner::AssocItem(item, ctxt);
visit::walk_assoc_item(self, item, ctxt);
}
fn visit_foreign_item(&mut self, item: &'a ast::ForeignItem) {
let def_id = self.node_id_to_def_id[&item.id];
- self.index.ensure_contains_elem(def_id, || AstOwner::NonOwner);
- self.index[def_id] = AstOwner::ForeignItem(item);
+ *self.index.ensure_contains_elem(def_id, || AstOwner::NonOwner) =
+ AstOwner::ForeignItem(item);
visit::walk_foreign_item(self, item);
}
}
@@ -438,6 +435,7 @@ pub fn lower_to_hir(tcx: TyCtxt<'_>, (): ()) -> hir::Crate<'_> {
// Queries that borrow `resolver_for_lowering`.
tcx.ensure_with_value().output_filenames(());
tcx.ensure_with_value().early_lint_checks(());
+ tcx.ensure_with_value().debugger_visualizers(LOCAL_CRATE);
let (mut resolver, krate) = tcx.resolver_for_lowering(()).steal();
let ast_index = index_crate(&resolver.node_id_to_def_id, &krate);
@@ -1193,13 +1191,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
// 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.
- TyKind::Path(qself, path) => {
+ TyKind::Path(None, path) => {
if let Some(res) = self
.resolver
.get_partial_res(ty.id)
.and_then(|partial_res| partial_res.full_res())
{
- if !res.matches_ns(Namespace::TypeNS) {
+ if !res.matches_ns(Namespace::TypeNS)
+ && path.is_potential_trivial_const_arg()
+ {
debug!(
"lower_generic_arg: Lowering type argument as const argument: {:?}",
ty,
@@ -1221,7 +1221,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
let path_expr = Expr {
id: ty.id,
- kind: ExprKind::Path(qself.clone(), path.clone()),
+ kind: ExprKind::Path(None, path.clone()),
span,
attrs: AttrVec::new(),
tokens: None,
@@ -1371,13 +1371,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
this.arena.alloc_from_iter(bounds.iter().filter_map(|bound| match bound {
GenericBound::Trait(
ty,
- TraitBoundModifier::None | TraitBoundModifier::MaybeConst,
+ TraitBoundModifier::None
+ | TraitBoundModifier::MaybeConst
+ | TraitBoundModifier::Negative,
) => Some(this.lower_poly_trait_ref(ty, itctx)),
// `~const ?Bound` will cause an error during AST validation
// anyways, so treat it like `?Bound` as compilation proceeds.
GenericBound::Trait(
_,
- TraitBoundModifier::Maybe | TraitBoundModifier::MaybeConstMaybe,
+ TraitBoundModifier::Maybe
+ | TraitBoundModifier::MaybeConstMaybe
+ | TraitBoundModifier::MaybeConstNegative,
) => None,
GenericBound::Outlives(lifetime) => {
if lifetime_bound.is_none() {
@@ -1404,14 +1408,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
*in_trait,
itctx,
),
- ImplTraitContext::TypeAliasesOpaqueTy => self.lower_opaque_impl_trait(
- span,
- hir::OpaqueTyOrigin::TyAlias,
- *def_node_id,
- bounds,
- false,
- itctx,
- ),
+ &ImplTraitContext::TypeAliasesOpaqueTy { in_assoc_ty } => self
+ .lower_opaque_impl_trait(
+ span,
+ hir::OpaqueTyOrigin::TyAlias { in_assoc_ty },
+ *def_node_id,
+ bounds,
+ false,
+ itctx,
+ ),
ImplTraitContext::Universal => {
let span = t.span;
self.create_def(
@@ -1420,7 +1425,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
DefPathData::ImplTrait,
span,
);
- let ident = Ident::from_str_and_span(&pprust::ty_to_string(t), span);
+
+ // 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,
+ );
+
let (param, bounds, path) = self.lower_universal_param_and_bounds(
*def_node_id,
span,
@@ -1476,6 +1490,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
/// Given a function definition like:
///
/// ```rust
+ /// use std::fmt::Debug;
+ ///
/// fn test<'a, T: Debug>(x: &'a T) -> impl Debug + 'a {
/// x
/// }
@@ -1483,13 +1499,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
///
/// we will create a TAIT definition in the HIR like
///
- /// ```
+ /// ```rust,ignore (pseudo-Rust)
/// type TestReturn<'a, T, 'x> = impl Debug + 'x
/// ```
///
/// and return a type like `TestReturn<'static, T, 'a>`, so that the function looks like:
///
- /// ```rust
+ /// ```rust,ignore (pseudo-Rust)
/// fn test<'a, T: Debug>(x: &'a T) -> TestReturn<'static, T, 'a>
/// ```
///
@@ -1529,13 +1545,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
// If this came from a TAIT (as opposed to a function that returns an RPIT), we only want
// to capture the lifetimes that appear in the bounds. So visit the bounds to find out
// exactly which ones those are.
- let lifetimes_to_remap = if origin == hir::OpaqueTyOrigin::TyAlias {
- // in a TAIT like `type Foo<'a> = impl Foo<'a>`, we don't keep all the lifetime parameters
- Vec::new()
- } else {
- // in fn return position, like the `fn test<'a>() -> impl Debug + 'a` example,
- // we only keep the lifetimes that appear in the `impl Debug` itself:
- lifetime_collector::lifetimes_in_bounds(&self.resolver, bounds)
+ let lifetimes_to_remap = match origin {
+ hir::OpaqueTyOrigin::TyAlias { .. } => {
+ // in a TAIT like `type Foo<'a> = impl Foo<'a>`, we don't keep all the lifetime parameters
+ Vec::new()
+ }
+ hir::OpaqueTyOrigin::AsyncFn(..) | hir::OpaqueTyOrigin::FnReturn(..) => {
+ // in fn return position, like the `fn test<'a>() -> impl Debug + 'a` example,
+ // we only keep the lifetimes that appear in the `impl Debug` itself:
+ lifetime_collector::lifetimes_in_bounds(&self.resolver, bounds)
+ }
};
debug!(?lifetimes_to_remap);
@@ -2424,11 +2443,20 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
TraitBoundModifier::None => hir::TraitBoundModifier::None,
TraitBoundModifier::MaybeConst => hir::TraitBoundModifier::MaybeConst,
+ TraitBoundModifier::Negative => {
+ if self.tcx.features().negative_bounds {
+ hir::TraitBoundModifier::Negative
+ } else {
+ hir::TraitBoundModifier::None
+ }
+ }
+
// `MaybeConstMaybe` will cause an error during AST validation, but we need to pick a
// placeholder for compilation to proceed.
TraitBoundModifier::MaybeConstMaybe | TraitBoundModifier::Maybe => {
hir::TraitBoundModifier::Maybe
}
+ TraitBoundModifier::MaybeConstNegative => hir::TraitBoundModifier::MaybeConst,
}
}
diff --git a/compiler/rustc_ast_lowering/src/path.rs b/compiler/rustc_ast_lowering/src/path.rs
index 8eb84c036..441282c05 100644
--- a/compiler/rustc_ast_lowering/src/path.rs
+++ b/compiler/rustc_ast_lowering/src/path.rs
@@ -136,7 +136,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
self.diagnostic().span_bug(
p.span,
- &format!(
+ format!(
"lower_qpath: no final extension segment in {}..{}",
proj_start,
p.segments.len()