Skip to content

Commit

Permalink
Auto merge of #118602 - TaKO8Ki:rollup-njcouns, r=TaKO8Ki
Browse files Browse the repository at this point in the history
Rollup of 5 pull requests

Successful merges:

 - #118495 (Restrict what symbols can be used in `#[diagnostic::on_unimplemented]` format strings)
 - #118540 (codegen, miri: fix computing the offset of an unsized field in a packed struct)
 - #118551 (more targeted errors when extern types end up in places they should not)
 - #118573 (rustc: Harmonize `DefKind` and `DefPathData`)
 - #118586 (Improve example in `slice::windows()` doc)

r? `@ghost`
`@rustbot` modify labels: rollup
  • Loading branch information
bors committed Dec 4, 2023
2 parents 0a83e43 + f1397e6 commit e281163
Show file tree
Hide file tree
Showing 33 changed files with 854 additions and 209 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_ast_lowering/src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use rustc_ast::*;
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::definitions::DefPathData;
use rustc_session::parse::feature_err;
use rustc_span::symbol::kw;
use rustc_span::{sym, Span};
use rustc_target::asm;
use std::collections::hash_map::Entry;
Expand Down Expand Up @@ -227,7 +227,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
self.create_def(
parent_def_id.def_id,
node_id,
DefPathData::AnonConst,
kw::Empty,
DefKind::AnonConst,
*op_sp,
);
Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ use rustc_ast::*;
use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::definitions::DefPathData;
use rustc_session::errors::report_lit_error;
use rustc_span::source_map::{respan, Spanned};
use rustc_span::symbol::{sym, Ident, Symbol};
use rustc_span::symbol::{kw, sym, Ident, Symbol};
use rustc_span::DUMMY_SP;
use rustc_span::{DesugaringKind, Span};
use thin_vec::{thin_vec, ThinVec};
Expand Down Expand Up @@ -376,7 +375,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
self.create_def(
parent_def_id.def_id,
node_id,
DefPathData::AnonConst,
kw::Empty,
DefKind::AnonConst,
f.span,
);
Expand Down
12 changes: 3 additions & 9 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use super::ResolverAstLoweringExt;
use super::{AstOwner, ImplTraitContext, ImplTraitPosition};
use super::{FnDeclKind, LoweringContext, ParamMode};

use hir::definitions::DefPathData;
use rustc_ast::ptr::P;
use rustc_ast::visit::AssocCtxt;
use rustc_ast::*;
Expand Down Expand Up @@ -1367,7 +1366,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let def_id = self.create_def(
self.local_def_id(parent_node_id),
param_node_id,
DefPathData::TypeNs(sym::host),
sym::host,
DefKind::ConstParam,
span,
);
Expand Down Expand Up @@ -1427,13 +1426,8 @@ impl<'hir> LoweringContext<'_, 'hir> {

if let Some((span, hir_id, def_id)) = host_param_parts {
let const_node_id = self.next_node_id();
let anon_const = self.create_def(
def_id,
const_node_id,
DefPathData::AnonConst,
DefKind::AnonConst,
span,
);
let anon_const =
self.create_def(def_id, const_node_id, kw::Empty, DefKind::AnonConst, span);

let const_id = self.next_id();
let const_expr_id = self.next_id();
Expand Down
21 changes: 10 additions & 11 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ use rustc_errors::{DiagnosticArgFromDisplay, StashKey};
use rustc_hir as hir;
use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
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::{Idx, IndexSlice, IndexVec};
use rustc_middle::{
Expand Down Expand Up @@ -499,20 +498,20 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
&mut self,
parent: LocalDefId,
node_id: ast::NodeId,
data: DefPathData,
name: Symbol,
def_kind: DefKind,
span: Span,
) -> LocalDefId {
debug_assert_ne!(node_id, ast::DUMMY_NODE_ID);
assert!(
self.opt_local_def_id(node_id).is_none(),
"adding a def'n for node-id {:?} and data {:?} but a previous def'n exists: {:?}",
"adding a def'n for node-id {:?} and def kind {:?} but a previous def'n exists: {:?}",
node_id,
data,
def_kind,
self.tcx.hir().def_key(self.local_def_id(node_id)),
);

let def_id = self.tcx.at(span).create_def(parent, data, def_kind).def_id();
let def_id = self.tcx.at(span).create_def(parent, name, def_kind).def_id();

debug!("create_def: def_id_to_node_id[{:?}] <-> {:?}", def_id, node_id);
self.resolver.node_id_to_def_id.insert(node_id, def_id);
Expand Down Expand Up @@ -809,7 +808,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
let _def_id = self.create_def(
self.current_hir_id_owner.def_id,
param,
DefPathData::LifetimeNs(kw::UnderscoreLifetime),
kw::UnderscoreLifetime,
DefKind::LifetimeParam,
ident.span,
);
Expand Down Expand Up @@ -1227,7 +1226,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
let def_id = self.create_def(
parent_def_id.def_id,
node_id,
DefPathData::AnonConst,
kw::Empty,
DefKind::AnonConst,
span,
);
Expand Down Expand Up @@ -1465,7 +1464,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
self.create_def(
self.current_hir_id_owner.def_id,
*def_node_id,
DefPathData::TypeNs(ident.name),
ident.name,
DefKind::TyParam,
span,
);
Expand Down Expand Up @@ -1619,7 +1618,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
let opaque_ty_def_id = self.create_def(
self.current_hir_id_owner.def_id,
opaque_ty_node_id,
DefPathData::ImplTrait,
kw::Empty,
DefKind::OpaqueTy,
opaque_ty_span,
);
Expand Down Expand Up @@ -1674,7 +1673,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
let duplicated_lifetime_def_id = self.create_def(
opaque_ty_def_id,
duplicated_lifetime_node_id,
DefPathData::LifetimeNs(lifetime.ident.name),
lifetime.ident.name,
DefKind::LifetimeParam,
lifetime.ident.span,
);
Expand Down Expand Up @@ -2549,7 +2548,7 @@ impl<'hir> GenericArgsCtor<'hir> {
let def_id = lcx.create_def(
lcx.current_hir_id_owner.def_id,
id,
DefPathData::AnonConst,
kw::Empty,
DefKind::AnonConst,
span,
);
Expand Down
9 changes: 9 additions & 0 deletions compiler/rustc_codegen_llvm/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,15 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {

#[instrument(level = "trace", skip(self))]
fn load_operand(&mut self, place: PlaceRef<'tcx, &'ll Value>) -> OperandRef<'tcx, &'ll Value> {
if place.layout.is_unsized() {
let tail = self.tcx.struct_tail_with_normalize(place.layout.ty, |ty| ty, || {});
if matches!(tail.kind(), ty::Foreign(..)) {
// Unsized locals and, at least conceptually, even unsized arguments must be copied
// around, which requires dynamically determining their size. Therefore, we cannot
// allow `extern` types here. Consult t-opsem before removing this check.
panic!("unsized locals must not be `extern` types");
}
}
assert_eq!(place.llextra.is_some(), place.layout.is_unsized());

if place.layout.is_zst() {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ fn push_unqualified_item_name(
DefPathData::CrateRoot => {
output.push_str(tcx.crate_name(def_id.krate).as_str());
}
DefPathData::ClosureExpr => {
DefPathData::Closure => {
let label = coroutine_kind_label(tcx.coroutine_kind(def_id));

push_disambiguated_special_name(
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_codegen_ssa/src/mir/operand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ impl<'a, 'tcx, V: CodegenObject> OperandValue<V> {
// value is through `undef`/`poison`, and the store itself is useless.
}
OperandValue::Ref(r, None, source_align) => {
assert!(dest.layout.is_sized(), "cannot directly store unsized values");
if flags.contains(MemFlags::NONTEMPORAL) {
// HACK(nox): This is inefficient but there is no nontemporal memcpy.
let ty = bx.backend_type(dest.layout);
Expand Down
22 changes: 12 additions & 10 deletions compiler/rustc_codegen_ssa/src/mir/place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
// Simple cases, which don't need DST adjustment:
// * no metadata available - just log the case
// * known alignment - sized types, `[T]`, `str` or a foreign type
// * packed struct - there is no alignment padding
// Note that looking at `field.align` is incorrect since that is not necessarily equal
// to the dynamic alignment of the type.
match field.ty.kind() {
_ if self.llextra.is_none() => {
debug!(
Expand All @@ -154,14 +155,6 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
}
_ if field.is_sized() => return simple(),
ty::Slice(..) | ty::Str | ty::Foreign(..) => return simple(),
ty::Adt(def, _) => {
if def.repr().packed() {
// FIXME(eddyb) generalize the adjustment when we
// start supporting packing to larger alignments.
assert_eq!(self.layout.align.abi.bytes(), 1);
return simple();
}
}
_ => {}
}

Expand All @@ -186,7 +179,16 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
let unaligned_offset = bx.cx().const_usize(offset.bytes());

// Get the alignment of the field
let (_, unsized_align) = glue::size_and_align_of_dst(bx, field.ty, meta);
let (_, mut unsized_align) = glue::size_and_align_of_dst(bx, field.ty, meta);

// For packed types, we need to cap alignment.
if let ty::Adt(def, _) = self.layout.ty.kind()
&& let Some(packed) = def.repr().pack
{
let packed = bx.const_usize(packed.bytes());
let cmp = bx.icmp(IntPredicate::IntULT, unsized_align, packed);
unsized_align = bx.select(cmp, unsized_align, packed)
}

// Bump the unaligned offset up to the appropriate alignment
let offset = round_up_const_value_to_alignment(bx, unaligned_offset, unsized_align);
Expand Down
6 changes: 2 additions & 4 deletions compiler/rustc_const_eval/src/interpret/eval_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,7 @@ impl<'mir, 'tcx, Prov: Provenance, Extra> Frame<'mir, 'tcx, Prov, Extra> {
impl<'tcx> fmt::Display for FrameInfo<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
ty::tls::with(|tcx| {
if tcx.def_key(self.instance.def_id()).disambiguated_data.data
== DefPathData::ClosureExpr
{
if tcx.def_key(self.instance.def_id()).disambiguated_data.data == DefPathData::Closure {
write!(f, "inside closure")
} else {
// Note: this triggers a `good_path_delayed_bug` state, which means that if we ever
Expand All @@ -299,7 +297,7 @@ impl<'tcx> fmt::Display for FrameInfo<'tcx> {
impl<'tcx> FrameInfo<'tcx> {
pub fn as_note(&self, tcx: TyCtxt<'tcx>) -> errors::FrameNote {
let span = self.span;
if tcx.def_key(self.instance.def_id()).disambiguated_data.data == DefPathData::ClosureExpr {
if tcx.def_key(self.instance.def_id()).disambiguated_data.data == DefPathData::Closure {
errors::FrameNote { where_: "closure", span, instance: String::new(), times: 0 }
} else {
let instance = format!("{}", self.instance);
Expand Down
12 changes: 11 additions & 1 deletion compiler/rustc_const_eval/src/interpret/projection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,17 @@ where
// With custom DSTS, this *will* execute user-defined code, but the same
// happens at run-time so that's okay.
match self.size_and_align_of(&base_meta, &field_layout)? {
Some((_, align)) => (base_meta, offset.align_to(align)),
Some((_, align)) => {
// For packed types, we need to cap alignment.
let align = if let ty::Adt(def, _) = base.layout().ty.kind()
&& let Some(packed) = def.repr().pack
{
align.min(packed)
} else {
align
};
(base_meta, offset.align_to(align))
}
None => {
// For unsized types with an extern type tail we perform no adjustments.
// NOTE: keep this in sync with `PlaceRef::project_field` in the codegen backend.
Expand Down
37 changes: 37 additions & 0 deletions compiler/rustc_hir/src/def.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::definitions::DefPathData;
use crate::hir;

use rustc_ast as ast;
Expand Down Expand Up @@ -45,6 +46,7 @@ pub enum NonMacroAttrKind {
}

/// What kind of definition something is; e.g., `mod` vs `struct`.
/// `enum DefPathData` may need to be updated if a new variant is added here.
#[derive(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Debug, HashStable_Generic)]
pub enum DefKind {
// Type namespace
Expand Down Expand Up @@ -221,6 +223,41 @@ impl DefKind {
}
}

pub fn def_path_data(self, name: Symbol) -> DefPathData {
match self {
DefKind::Mod
| DefKind::Struct
| DefKind::Union
| DefKind::Enum
| DefKind::Variant
| DefKind::Trait
| DefKind::TyAlias
| DefKind::ForeignTy
| DefKind::TraitAlias
| DefKind::AssocTy
| DefKind::TyParam
| DefKind::ExternCrate => DefPathData::TypeNs(name),
DefKind::Fn
| DefKind::Const
| DefKind::ConstParam
| DefKind::Static(..)
| DefKind::AssocFn
| DefKind::AssocConst
| DefKind::Field => DefPathData::ValueNs(name),
DefKind::Macro(..) => DefPathData::MacroNs(name),
DefKind::LifetimeParam => DefPathData::LifetimeNs(name),
DefKind::Ctor(..) => DefPathData::Ctor,
DefKind::Use => DefPathData::Use,
DefKind::ForeignMod => DefPathData::ForeignMod,
DefKind::AnonConst => DefPathData::AnonConst,
DefKind::InlineConst => DefPathData::AnonConst,
DefKind::OpaqueTy => DefPathData::OpaqueTy,
DefKind::GlobalAsm => DefPathData::GlobalAsm,
DefKind::Impl { .. } => DefPathData::Impl,
DefKind::Closure => DefPathData::Closure,
}
}

#[inline]
pub fn is_fn_like(self) -> bool {
matches!(self, DefKind::Fn | DefKind::AssocFn | DefKind::Closure)
Expand Down
18 changes: 9 additions & 9 deletions compiler/rustc_hir/src/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ impl DefPath {
}
}

/// New variants should only be added in synchronization with `enum DefKind`.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Encodable, Decodable)]
pub enum DefPathData {
// Root: these should only be used for the root nodes, because
Expand All @@ -271,7 +272,7 @@ pub enum DefPathData {
/// Something in the lifetime namespace.
LifetimeNs(Symbol),
/// A closure expression.
ClosureExpr,
Closure,

// Subportions of items:
/// Implicit constructor for a unit or tuple-like struct or enum variant.
Expand All @@ -280,9 +281,7 @@ pub enum DefPathData {
AnonConst,
/// An existential `impl Trait` type node.
/// Argument position `impl Trait` have a `TypeNs` with their pretty-printed name.
ImplTrait,
/// `impl Trait` generated associated type node.
ImplTraitAssocTy,
OpaqueTy,
}

impl Definitions {
Expand Down Expand Up @@ -403,16 +402,17 @@ impl DefPathData {
pub fn get_opt_name(&self) -> Option<Symbol> {
use self::DefPathData::*;
match *self {
TypeNs(name) if name == kw::Empty => None,
TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) => Some(name),

Impl | ForeignMod | CrateRoot | Use | GlobalAsm | ClosureExpr | Ctor | AnonConst
| ImplTrait | ImplTraitAssocTy => None,
Impl | ForeignMod | CrateRoot | Use | GlobalAsm | Closure | Ctor | AnonConst
| OpaqueTy => None,
}
}

pub fn name(&self) -> DefPathDataName {
use self::DefPathData::*;
match *self {
TypeNs(name) if name == kw::Empty => DefPathDataName::Anon { namespace: sym::opaque },
TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) => {
DefPathDataName::Named(name)
}
Expand All @@ -422,10 +422,10 @@ impl DefPathData {
ForeignMod => DefPathDataName::Anon { namespace: kw::Extern },
Use => DefPathDataName::Anon { namespace: kw::Use },
GlobalAsm => DefPathDataName::Anon { namespace: sym::global_asm },
ClosureExpr => DefPathDataName::Anon { namespace: sym::closure },
Closure => DefPathDataName::Anon { namespace: sym::closure },
Ctor => DefPathDataName::Anon { namespace: sym::constructor },
AnonConst => DefPathDataName::Anon { namespace: sym::constant },
ImplTrait | ImplTraitAssocTy => DefPathDataName::Anon { namespace: sym::opaque },
OpaqueTy => DefPathDataName::Anon { namespace: sym::opaque },
}
}
}
Expand Down
Loading

0 comments on commit e281163

Please sign in to comment.