Skip to content

Commit 23982a2

Browse files
committed
refactor: more general terminology and use existing attr parse machinery
1 parent fe52f7c commit 23982a2

9 files changed

Lines changed: 253 additions & 132 deletions

File tree

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
4848
use rustc_data_structures::steal::Steal;
4949
use rustc_data_structures::tagged_ptr::TaggedRef;
5050
use rustc_errors::{DiagArgFromDisplay, DiagCtxtHandle};
51-
use rustc_hir::attrs::AttrConstResolution;
51+
use rustc_hir::attrs::AttrResolution;
5252
use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
5353
use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE, LocalDefId};
5454
use rustc_hir::definitions::{DefPathData, DisambiguatorState};
@@ -156,7 +156,7 @@ struct LoweringContext<'a, 'hir, R> {
156156
impl<'a, 'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'a, 'hir, R> {
157157
fn new(tcx: TyCtxt<'hir>, resolver: &'a mut R) -> Self {
158158
let registered_tools = tcx.registered_tools(()).iter().map(|x| x.name).collect();
159-
let attr_const_res_map = resolver.all_attr_const_resolutions();
159+
let attr_res_map = resolver.all_attr_resolutions();
160160
Self {
161161
tcx,
162162
resolver,
@@ -211,7 +211,7 @@ impl<'a, 'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'a, 'hir, R> {
211211
tcx.sess,
212212
tcx.features(),
213213
registered_tools,
214-
attr_const_res_map,
214+
attr_res_map,
215215
Late,
216216
),
217217
delayed_lints: Vec::new(),
@@ -242,7 +242,7 @@ impl SpanLowerer {
242242
struct ResolverDelayedAstLowering<'a, 'tcx> {
243243
node_id_to_def_id: NodeMap<LocalDefId>,
244244
partial_res_map: NodeMap<PartialRes>,
245-
attr_const_res_map: FxIndexMap<rustc_span::AttrId, Vec<AttrConstResolution<ast::NodeId>>>,
245+
attr_res_map: FxIndexMap<rustc_span::AttrId, Vec<AttrResolution<ast::NodeId>>>,
246246
next_node_id: NodeId,
247247
base: &'a ResolverAstLowering<'tcx>,
248248
}
@@ -257,11 +257,11 @@ impl<'a, 'tcx> ResolverAstLoweringExt<'tcx> for ResolverDelayedAstLowering<'a, '
257257
self.partial_res_map.get(&id).copied().or_else(|| self.base.get_partial_res(id))
258258
}
259259

260-
fn all_attr_const_resolutions(
260+
fn all_attr_resolutions(
261261
&self,
262-
) -> FxIndexMap<rustc_span::AttrId, Vec<AttrConstResolution<ast::NodeId>>> {
263-
let mut map = self.base.all_attr_const_resolutions();
264-
for (attr_id, resolutions) in &self.attr_const_res_map {
262+
) -> FxIndexMap<rustc_span::AttrId, Vec<AttrResolution<ast::NodeId>>> {
263+
let mut map = self.base.all_attr_resolutions();
264+
for (attr_id, resolutions) in &self.attr_res_map {
265265
map.entry(*attr_id).or_default().extend(resolutions.iter().copied());
266266
}
267267
map
@@ -359,10 +359,10 @@ impl<'tcx> ResolverAstLowering<'tcx> {
359359
self.partial_res_map.get(&id).copied()
360360
}
361361

362-
fn all_attr_const_resolutions(
362+
fn all_attr_resolutions(
363363
&self,
364-
) -> FxIndexMap<rustc_span::AttrId, Vec<AttrConstResolution<ast::NodeId>>> {
365-
self.attr_const_res_map.clone()
364+
) -> FxIndexMap<rustc_span::AttrId, Vec<AttrResolution<ast::NodeId>>> {
365+
self.attr_res_map.clone()
366366
}
367367

368368
/// Obtains per-namespace resolutions for `use` statement with the given `NodeId`.
@@ -683,7 +683,7 @@ pub fn lower_delayed_owner(tcx: TyCtxt<'_>, def_id: LocalDefId) {
683683
let mut resolver = ResolverDelayedAstLowering {
684684
next_node_id: resolver.next_node_id,
685685
partial_res_map: Default::default(),
686-
attr_const_res_map: Default::default(),
686+
attr_res_map: Default::default(),
687687
node_id_to_def_id: Default::default(),
688688
base: resolver,
689689
};

compiler/rustc_attr_parsing/src/attributes/repr.rs

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_abi::{Align, Size};
22
use rustc_ast::{IntTy, LitIntType, LitKind, UintTy};
3-
use rustc_hir::attrs::{AttrConstResolved, AttrIntValue, IntType, ReprAttr};
3+
use rustc_hir::attrs::{AttrIntValue, AttrResolutionKind, AttrResolved, IntType, ReprAttr};
44
use rustc_hir::def::{DefKind, Res};
55
use rustc_session::parse::feature_err;
66

@@ -105,7 +105,10 @@ fn int_type_of_word(s: Symbol) -> Option<IntType> {
105105
}
106106
}
107107

108-
fn parse_repr<S: Stage>(cx: &AcceptContext<'_, '_, S>, param: &MetaItemParser) -> Option<ReprAttr> {
108+
fn parse_repr<S: Stage>(
109+
cx: &mut AcceptContext<'_, '_, S>,
110+
param: &MetaItemParser,
111+
) -> Option<ReprAttr> {
109112
use ReprAttr::*;
110113

111114
// FIXME(jdonszelmann): invert the parsing here to match on the word first and then the
@@ -200,7 +203,7 @@ enum AlignmentParseError {
200203
}
201204

202205
fn parse_repr_align<S: Stage>(
203-
cx: &AcceptContext<'_, '_, S>,
206+
cx: &mut AcceptContext<'_, '_, S>,
204207
list: &MetaItemListParser,
205208
param_span: Span,
206209
align_kind: AlignKind,
@@ -283,7 +286,7 @@ fn parse_alignment<S: Stage>(
283286
}
284287

285288
fn parse_alignment_or_const_path<S: Stage>(
286-
cx: &AcceptContext<'_, '_, S>,
289+
cx: &mut AcceptContext<'_, '_, S>,
287290
arg: &MetaItemOrLitParser,
288291
attr_name: &'static str,
289292
) -> Result<AttrIntValue, AlignmentParseError> {
@@ -301,10 +304,15 @@ fn parse_alignment_or_const_path<S: Stage>(
301304
return Err(AlignmentParseError::Message("not an unsuffixed integer".to_string()));
302305
}
303306

304-
if let Some(features) = cx.features_option()
305-
&& !features.const_attr_paths()
306-
&& !meta.span().allows_unstable(sym::const_attr_paths)
307-
{
307+
let path_span = meta.path().span();
308+
let feature_enabled = cx.features_option().is_some_and(|features| features.const_attr_paths())
309+
|| path_span.allows_unstable(sym::const_attr_paths);
310+
311+
if !feature_enabled {
312+
if matches!(cx.stage.should_emit(), ShouldEmit::Nothing) {
313+
return Ok(AttrIntValue::Lit(1));
314+
}
315+
308316
feature_err(
309317
cx.sess(),
310318
sym::const_attr_paths,
@@ -315,7 +323,9 @@ fn parse_alignment_or_const_path<S: Stage>(
315323
return Err(AlignmentParseError::AlreadyErrored);
316324
}
317325

318-
let Some(resolution) = cx.attr_const_resolution(meta.path().span()) else {
326+
cx.record_attr_resolution_request(AttrResolutionKind::Const, meta.path().0.clone());
327+
328+
let Some(resolution) = cx.attr_resolution(AttrResolutionKind::Const, path_span) else {
319329
// `parse_limited(sym::repr)` runs before lowering for callers that only care whether
320330
// `repr(packed(...))` exists at all.
321331
if matches!(cx.stage.should_emit(), ShouldEmit::Nothing) {
@@ -325,22 +335,18 @@ fn parse_alignment_or_const_path<S: Stage>(
325335
};
326336

327337
match resolution {
328-
AttrConstResolved::Resolved(Res::Def(DefKind::Const { .. }, def_id)) => {
329-
Ok(AttrIntValue::Const { def_id, span: meta.path().span() })
338+
AttrResolved::Resolved(Res::Def(DefKind::Const { .. }, def_id)) => {
339+
Ok(AttrIntValue::Const { def_id, span: path_span })
330340
}
331-
AttrConstResolved::Resolved(Res::Def(DefKind::ConstParam, _)) => {
332-
cx.emit_err(AttrConstGenericNotSupported { span: meta.path().span(), attr_name });
341+
AttrResolved::Resolved(Res::Def(DefKind::ConstParam, _)) => {
342+
cx.emit_err(AttrConstGenericNotSupported { span: path_span, attr_name });
333343
Err(AlignmentParseError::AlreadyErrored)
334344
}
335-
AttrConstResolved::Resolved(res) => {
336-
cx.emit_err(AttrConstPathNotConst {
337-
span: meta.path().span(),
338-
attr_name,
339-
thing: res.descr(),
340-
});
345+
AttrResolved::Resolved(res) => {
346+
cx.emit_err(AttrConstPathNotConst { span: path_span, attr_name, thing: res.descr() });
341347
Err(AlignmentParseError::AlreadyErrored)
342348
}
343-
AttrConstResolved::Error => Err(AlignmentParseError::AlreadyErrored),
349+
AttrResolved::Error => Err(AlignmentParseError::AlreadyErrored),
344350
}
345351
}
346352

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use private::Sealed;
99
use rustc_ast::{AttrStyle, MetaItemLit, NodeId};
1010
use rustc_errors::{Diag, Diagnostic, Level};
1111
use rustc_feature::{AttrSuggestionStyle, AttributeTemplate};
12-
use rustc_hir::attrs::AttributeKind;
12+
use rustc_hir::attrs::{AttrResolutionKind, AttributeKind};
1313
use rustc_hir::lints::AttributeLintKind;
1414
use rustc_hir::{AttrPath, HirId};
1515
use rustc_parse::parser::Recovery;
@@ -498,13 +498,27 @@ impl<'f, 'sess: 'f, S: Stage> SharedContext<'f, 'sess, S> {
498498
impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
499499
pub(crate) fn adcx(&mut self) -> AttributeDiagnosticContext<'_, 'f, 'sess, S> {
500500
AttributeDiagnosticContext { ctx: self, custom_suggestions: Vec::new() }
501+
}
501502

502-
pub(crate) fn attr_const_resolution(
503+
pub(crate) fn attr_resolution(
503504
&self,
505+
kind: AttrResolutionKind,
504506
path_span: Span,
505-
) -> Option<rustc_hir::attrs::AttrConstResolved<NodeId>> {
506-
self.attr_id.and_then(|attr_id| self.shared.cx.attr_const_resolution(attr_id, path_span))
507+
) -> Option<rustc_hir::attrs::AttrResolved<NodeId>> {
508+
self.attr_id.and_then(|attr_id| self.shared.cx.attr_resolution(attr_id, kind, path_span))
507509
}
510+
511+
pub(crate) fn record_attr_resolution_request(
512+
&mut self,
513+
kind: AttrResolutionKind,
514+
path: rustc_ast::Path,
515+
) {
516+
if let Some(attr_id) = self.attr_id {
517+
self.shared.cx.record_attr_resolution_request(
518+
attr_id,
519+
crate::interface::AttrResolutionRequest { kind, path },
520+
);
521+
}
508522
}
509523
}
510524

0 commit comments

Comments
 (0)