Skip to content

Commit f0b7fcf

Browse files
committed
Auto merge of #150729 - matthiaskrgr:rollup-an2m4zg, r=matthiaskrgr
Rollup of 4 pull requests Successful merges: - #150026 (Fix macro_metavar_expr_concat behavior with nested repetitions) - #150521 (resolve: Rename "name bindings" to "name declarations") - #150704 (MGCA: Const constructors support) - #150728 (Cleanup some ui tests for const-traits) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 4d73a00 + 799c06f commit f0b7fcf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+948
-940
lines changed

compiler/rustc_expand/src/mbe/transcribe.rs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -558,25 +558,20 @@ fn metavar_expr_concat<'tx>(
558558
MetaVarExprConcatElem::Ident(elem) => elem.name,
559559
MetaVarExprConcatElem::Literal(elem) => *elem,
560560
MetaVarExprConcatElem::Var(ident) => {
561-
match matched_from_ident(dcx, *ident, tscx.interp)? {
562-
NamedMatch::MatchedSeq(named_matches) => {
563-
let Some((curr_idx, _)) = tscx.repeats.last() else {
564-
return Err(dcx.struct_span_err(dspan.entire(), "invalid syntax"));
565-
};
566-
match &named_matches[*curr_idx] {
567-
// FIXME(c410-f3r) Nested repetitions are unimplemented
568-
MatchedSeq(_) => {
569-
return Err(dcx.struct_span_err(
570-
ident.span,
571-
"nested repetitions with `${concat(...)}` metavariable expressions are not yet supported",
572-
));
573-
}
574-
MatchedSingle(pnr) => extract_symbol_from_pnr(dcx, pnr, ident.span)?,
575-
}
576-
}
577-
NamedMatch::MatchedSingle(pnr) => {
561+
let key = MacroRulesNormalizedIdent::new(*ident);
562+
match lookup_cur_matched(key, tscx.interp, &tscx.repeats) {
563+
Some(NamedMatch::MatchedSingle(pnr)) => {
578564
extract_symbol_from_pnr(dcx, pnr, ident.span)?
579565
}
566+
Some(NamedMatch::MatchedSeq(..)) => {
567+
return Err(dcx.struct_span_err(
568+
ident.span,
569+
"`${concat(...)}` variable is still repeating at this depth",
570+
));
571+
}
572+
None => {
573+
return Err(dcx.create_err(MveUnrecognizedVar { span: ident.span, key }));
574+
}
580575
}
581576
}
582577
};

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,9 +1415,15 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
14151415
let ct = self.check_param_uses_if_mcg(ct, span, false);
14161416
Ok(ct)
14171417
}
1418-
TypeRelativePath::Ctor { ctor_def_id, args } => {
1419-
return Ok(ty::Const::zero_sized(tcx, Ty::new_fn_def(tcx, ctor_def_id, args)));
1420-
}
1418+
TypeRelativePath::Ctor { ctor_def_id, args } => match tcx.def_kind(ctor_def_id) {
1419+
DefKind::Ctor(_, CtorKind::Fn) => {
1420+
Ok(ty::Const::zero_sized(tcx, Ty::new_fn_def(tcx, ctor_def_id, args)))
1421+
}
1422+
DefKind::Ctor(ctor_of, CtorKind::Const) => {
1423+
Ok(self.construct_const_ctor_value(ctor_def_id, ctor_of, args))
1424+
}
1425+
_ => unreachable!(),
1426+
},
14211427
// FIXME(mgca): implement support for this once ready to support all adt ctor expressions,
14221428
// not just const ctors
14231429
TypeRelativePath::Variant { .. } => {
@@ -1452,7 +1458,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
14521458
// FIXME(mgca): do we want constructor resolutions to take priority over
14531459
// other possible resolutions?
14541460
if matches!(mode, LowerTypeRelativePathMode::Const)
1455-
&& let Some((CtorKind::Fn, ctor_def_id)) = variant_def.ctor
1461+
&& let Some((_, ctor_def_id)) = variant_def.ctor
14561462
{
14571463
tcx.check_stability(variant_def.def_id, Some(qpath_hir_id), span, None);
14581464
let _ = self.prohibit_generic_args(
@@ -2597,14 +2603,29 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
25972603
);
25982604
self.lower_const_param(def_id, hir_id)
25992605
}
2600-
Res::Def(DefKind::Const | DefKind::Ctor(_, CtorKind::Const), did) => {
2606+
Res::Def(DefKind::Const, did) => {
26012607
assert_eq!(opt_self_ty, None);
26022608
let [leading_segments @ .., segment] = path.segments else { bug!() };
26032609
let _ = self
26042610
.prohibit_generic_args(leading_segments.iter(), GenericsArgsErrExtend::None);
26052611
let args = self.lower_generic_args_of_path_segment(span, did, segment);
26062612
ty::Const::new_unevaluated(tcx, ty::UnevaluatedConst::new(did, args))
26072613
}
2614+
Res::Def(DefKind::Ctor(ctor_of, CtorKind::Const), did) => {
2615+
assert_eq!(opt_self_ty, None);
2616+
let [leading_segments @ .., segment] = path.segments else { bug!() };
2617+
let _ = self
2618+
.prohibit_generic_args(leading_segments.iter(), GenericsArgsErrExtend::None);
2619+
2620+
let parent_did = tcx.parent(did);
2621+
let generics_did = match ctor_of {
2622+
CtorOf::Variant => tcx.parent(parent_did),
2623+
CtorOf::Struct => parent_did,
2624+
};
2625+
let args = self.lower_generic_args_of_path_segment(span, generics_did, segment);
2626+
2627+
self.construct_const_ctor_value(did, ctor_of, args)
2628+
}
26082629
Res::Def(DefKind::Ctor(_, CtorKind::Fn), did) => {
26092630
assert_eq!(opt_self_ty, None);
26102631
let [leading_segments @ .., segment] = path.segments else { bug!() };
@@ -3174,4 +3195,31 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
31743195
}
31753196
Some(r)
31763197
}
3198+
3199+
fn construct_const_ctor_value(
3200+
&self,
3201+
ctor_def_id: DefId,
3202+
ctor_of: CtorOf,
3203+
args: GenericArgsRef<'tcx>,
3204+
) -> Const<'tcx> {
3205+
let tcx = self.tcx();
3206+
let parent_did = tcx.parent(ctor_def_id);
3207+
3208+
let adt_def = tcx.adt_def(match ctor_of {
3209+
CtorOf::Variant => tcx.parent(parent_did),
3210+
CtorOf::Struct => parent_did,
3211+
});
3212+
3213+
let variant_idx = adt_def.variant_index_with_id(parent_did);
3214+
3215+
let valtree = if adt_def.is_enum() {
3216+
let discr = ty::ValTree::from_scalar_int(tcx, variant_idx.as_u32().into());
3217+
ty::ValTree::from_branches(tcx, [ty::Const::new_value(tcx, discr, tcx.types.u32)])
3218+
} else {
3219+
ty::ValTree::zst(tcx)
3220+
};
3221+
3222+
let adt_ty = Ty::new_adt(tcx, adt_def, args);
3223+
ty::Const::new_value(tcx, valtree, adt_ty)
3224+
}
31773225
}

compiler/rustc_middle/src/metadata.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ impl Reexport {
2626
}
2727
}
2828

29-
/// This structure is supposed to keep enough data to re-create `NameBinding`s for other crates
29+
/// This structure is supposed to keep enough data to re-create `Decl`s for other crates
3030
/// during name resolution. Right now the bindings are not recreated entirely precisely so we may
3131
/// need to add more data in the future to correctly support macros 2.0, for example.
3232
/// Module child can be either a proper item or a reexport (including private imports).

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 47 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -33,31 +33,33 @@ use tracing::debug;
3333
use crate::Namespace::{MacroNS, TypeNS, ValueNS};
3434
use crate::def_collector::collect_definitions;
3535
use crate::imports::{ImportData, ImportKind};
36-
use crate::macros::{MacroRulesBinding, MacroRulesScope, MacroRulesScopeRef};
36+
use crate::macros::{MacroRulesDecl, MacroRulesScope, MacroRulesScopeRef};
3737
use crate::ref_mut::CmCell;
3838
use crate::{
39-
BindingKey, ExternPreludeEntry, Finalize, MacroData, Module, ModuleKind, ModuleOrUniformRoot,
40-
NameBinding, NameBindingData, NameBindingKind, ParentScope, PathResult, ResolutionError,
41-
Resolver, Segment, Used, VisResolutionError, errors,
39+
BindingKey, Decl, DeclData, DeclKind, ExternPreludeEntry, Finalize, MacroData, Module,
40+
ModuleKind, ModuleOrUniformRoot, ParentScope, PathResult, ResolutionError, Resolver, Segment,
41+
Used, VisResolutionError, errors,
4242
};
4343

4444
type Res = def::Res<NodeId>;
4545

4646
impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
47-
/// Defines `name` in namespace `ns` of module `parent` to be `def` if it is not yet defined;
48-
/// otherwise, reports an error.
49-
pub(crate) fn define_binding_local(
47+
/// Attempt to put the declaration with the given name and namespace into the module,
48+
/// and report an error in case of a collision.
49+
pub(crate) fn plant_decl_into_local_module(
5050
&mut self,
5151
parent: Module<'ra>,
5252
ident: Ident,
5353
ns: Namespace,
54-
binding: NameBinding<'ra>,
54+
decl: Decl<'ra>,
5555
) {
56-
if let Err(old_binding) = self.try_define_local(parent, ident, ns, binding, false) {
57-
self.report_conflict(parent, ident, ns, old_binding, binding);
56+
if let Err(old_decl) = self.try_plant_decl_into_local_module(parent, ident, ns, decl, false)
57+
{
58+
self.report_conflict(parent, ident, ns, old_decl, decl);
5859
}
5960
}
6061

62+
/// Create a name definitinon from the given components, and put it into the local module.
6163
fn define_local(
6264
&mut self,
6365
parent: Module<'ra>,
@@ -68,10 +70,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
6870
span: Span,
6971
expn_id: LocalExpnId,
7072
) {
71-
let binding = self.arenas.new_res_binding(res, vis.to_def_id(), span, expn_id);
72-
self.define_binding_local(parent, ident, ns, binding);
73+
let decl = self.arenas.new_def_decl(res, vis.to_def_id(), span, expn_id);
74+
self.plant_decl_into_local_module(parent, ident, ns, decl);
7375
}
7476

77+
/// Create a name definitinon from the given components, and put it into the extern module.
7578
fn define_extern(
7679
&self,
7780
parent: Module<'ra>,
@@ -82,10 +85,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
8285
vis: Visibility<DefId>,
8386
span: Span,
8487
expansion: LocalExpnId,
85-
ambiguity: Option<NameBinding<'ra>>,
88+
ambiguity: Option<Decl<'ra>>,
8689
) {
87-
let binding = self.arenas.alloc_name_binding(NameBindingData {
88-
kind: NameBindingKind::Res(res),
90+
let decl = self.arenas.alloc_decl(DeclData {
91+
kind: DeclKind::Def(res),
8992
ambiguity,
9093
// External ambiguities always report the `AMBIGUOUS_GLOB_IMPORTS` lint at the moment.
9194
warn_ambiguity: true,
@@ -101,8 +104,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
101104
if self
102105
.resolution_or_default(parent, key)
103106
.borrow_mut_unchecked()
104-
.non_glob_binding
105-
.replace(binding)
107+
.non_glob_decl
108+
.replace(decl)
106109
.is_some()
107110
{
108111
span_bug!(span, "an external binding was already defined");
@@ -284,7 +287,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
284287
let ModChild { ident: _, res, vis, ref reexport_chain } = *ambig_child;
285288
let span = child_span(self, reexport_chain, res);
286289
let res = res.expect_non_local();
287-
self.arenas.new_res_binding(res, vis, span, expansion)
290+
self.arenas.new_def_decl(res, vis, span, expansion)
288291
});
289292

290293
// Record primary definitions.
@@ -691,7 +694,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
691694
let kind = ImportKind::Single {
692695
source: source.ident,
693696
target: ident,
694-
bindings: Default::default(),
697+
decls: Default::default(),
695698
type_ns_only,
696699
nested,
697700
id,
@@ -977,7 +980,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
977980
let parent_scope = self.parent_scope;
978981
let expansion = parent_scope.expansion;
979982

980-
let (used, module, binding) = if orig_name.is_none() && ident.name == kw::SelfLower {
983+
let (used, module, decl) = if orig_name.is_none() && ident.name == kw::SelfLower {
981984
self.r.dcx().emit_err(errors::ExternCrateSelfRequiresRenaming { span: sp });
982985
return;
983986
} else if orig_name == Some(kw::SelfLower) {
@@ -997,10 +1000,10 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
9971000
}
9981001
.map(|module| {
9991002
let used = self.process_macro_use_imports(item, module);
1000-
let binding = self.r.arenas.new_pub_res_binding(module.res().unwrap(), sp, expansion);
1001-
(used, Some(ModuleOrUniformRoot::Module(module)), binding)
1003+
let decl = self.r.arenas.new_pub_def_decl(module.res().unwrap(), sp, expansion);
1004+
(used, Some(ModuleOrUniformRoot::Module(module)), decl)
10021005
})
1003-
.unwrap_or((true, None, self.r.dummy_binding));
1006+
.unwrap_or((true, None, self.r.dummy_decl));
10041007
let import = self.r.arenas.alloc_import(ImportData {
10051008
kind: ImportKind::ExternCrate { source: orig_name, target: ident, id: item.id },
10061009
root_id: item.id,
@@ -1019,15 +1022,15 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
10191022
self.r.import_use_map.insert(import, Used::Other);
10201023
}
10211024
self.r.potentially_unused_imports.push(import);
1022-
let imported_binding = self.r.import(binding, import);
1025+
let import_decl = self.r.new_import_decl(decl, import);
10231026
if ident.name != kw::Underscore && parent == self.r.graph_root {
10241027
let norm_ident = Macros20NormalizedIdent::new(ident);
10251028
// FIXME: this error is technically unnecessary now when extern prelude is split into
10261029
// two scopes, remove it with lang team approval.
10271030
if let Some(entry) = self.r.extern_prelude.get(&norm_ident)
10281031
&& expansion != LocalExpnId::ROOT
10291032
&& orig_name.is_some()
1030-
&& entry.item_binding.is_none()
1033+
&& entry.item_decl.is_none()
10311034
{
10321035
self.r.dcx().emit_err(
10331036
errors::MacroExpandedExternCrateCannotShadowExternArguments { span: item.span },
@@ -1038,21 +1041,21 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
10381041
match self.r.extern_prelude.entry(norm_ident) {
10391042
Entry::Occupied(mut occupied) => {
10401043
let entry = occupied.get_mut();
1041-
if entry.item_binding.is_some() {
1044+
if entry.item_decl.is_some() {
10421045
let msg = format!("extern crate `{ident}` already in extern prelude");
10431046
self.r.tcx.dcx().span_delayed_bug(item.span, msg);
10441047
} else {
1045-
entry.item_binding = Some((imported_binding, orig_name.is_some()));
1048+
entry.item_decl = Some((import_decl, orig_name.is_some()));
10461049
}
10471050
entry
10481051
}
10491052
Entry::Vacant(vacant) => vacant.insert(ExternPreludeEntry {
1050-
item_binding: Some((imported_binding, true)),
1051-
flag_binding: None,
1053+
item_decl: Some((import_decl, true)),
1054+
flag_decl: None,
10521055
}),
10531056
};
10541057
}
1055-
self.r.define_binding_local(parent, ident, TypeNS, imported_binding);
1058+
self.r.plant_decl_into_local_module(parent, ident, TypeNS, import_decl);
10561059
}
10571060

10581061
/// Constructs the reduced graph for one foreign item.
@@ -1089,14 +1092,14 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
10891092
}
10901093
}
10911094

1092-
fn add_macro_use_binding(
1095+
fn add_macro_use_decl(
10931096
&mut self,
10941097
name: Symbol,
1095-
binding: NameBinding<'ra>,
1098+
decl: Decl<'ra>,
10961099
span: Span,
10971100
allow_shadowing: bool,
10981101
) {
1099-
if self.r.macro_use_prelude.insert(name, binding).is_some() && !allow_shadowing {
1102+
if self.r.macro_use_prelude.insert(name, decl).is_some() && !allow_shadowing {
11001103
self.r.dcx().emit_err(errors::MacroUseNameAlreadyInUse { span, name });
11011104
}
11021105
}
@@ -1167,8 +1170,8 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
11671170
}
11681171
macro_use_import(this, span, true)
11691172
};
1170-
let import_binding = this.r.import(binding, import);
1171-
this.add_macro_use_binding(ident.name, import_binding, span, allow_shadowing);
1173+
let import_decl = this.r.new_import_decl(binding, import);
1174+
this.add_macro_use_decl(ident.name, import_decl, span, allow_shadowing);
11721175
}
11731176
});
11741177
} else {
@@ -1183,13 +1186,8 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
11831186
if let Ok(binding) = result {
11841187
let import = macro_use_import(self, ident.span, false);
11851188
self.r.potentially_unused_imports.push(import);
1186-
let imported_binding = self.r.import(binding, import);
1187-
self.add_macro_use_binding(
1188-
ident.name,
1189-
imported_binding,
1190-
ident.span,
1191-
allow_shadowing,
1192-
);
1189+
let import_decl = self.r.new_import_decl(binding, import);
1190+
self.add_macro_use_decl(ident.name, import_decl, ident.span, allow_shadowing);
11931191
} else {
11941192
self.r.dcx().emit_err(errors::ImportedMacroNotFound { span: ident.span });
11951193
}
@@ -1300,8 +1298,8 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
13001298
} else {
13011299
Visibility::Restricted(CRATE_DEF_ID)
13021300
};
1303-
let binding = self.r.arenas.new_res_binding(res, vis.to_def_id(), span, expansion);
1304-
self.r.set_binding_parent_module(binding, parent_scope.module);
1301+
let decl = self.r.arenas.new_def_decl(res, vis.to_def_id(), span, expansion);
1302+
self.r.set_decl_parent_module(decl, parent_scope.module);
13051303
self.r.all_macro_rules.insert(ident.name);
13061304
if is_macro_export {
13071305
let import = self.r.arenas.alloc_import(ImportData {
@@ -1319,17 +1317,17 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
13191317
vis_span: item.vis.span,
13201318
});
13211319
self.r.import_use_map.insert(import, Used::Other);
1322-
let import_binding = self.r.import(binding, import);
1323-
self.r.define_binding_local(self.r.graph_root, ident, MacroNS, import_binding);
1320+
let import_decl = self.r.new_import_decl(decl, import);
1321+
self.r.plant_decl_into_local_module(self.r.graph_root, ident, MacroNS, import_decl);
13241322
} else {
13251323
self.r.check_reserved_macro_name(ident, res);
13261324
self.insert_unused_macro(ident, def_id, item.id);
13271325
}
13281326
self.r.feed_visibility(feed, vis);
1329-
let scope = self.r.arenas.alloc_macro_rules_scope(MacroRulesScope::Binding(
1330-
self.r.arenas.alloc_macro_rules_binding(MacroRulesBinding {
1327+
let scope = self.r.arenas.alloc_macro_rules_scope(MacroRulesScope::Def(
1328+
self.r.arenas.alloc_macro_rules_decl(MacroRulesDecl {
13311329
parent_macro_rules_scope: parent_scope.macro_rules,
1332-
binding,
1330+
decl,
13331331
ident,
13341332
}),
13351333
));

0 commit comments

Comments
 (0)