Skip to content

Commit 291c884

Browse files
committed
fix tests related to attr parsing
1 parent 854a049 commit 291c884

File tree

14 files changed

+97
-126
lines changed

14 files changed

+97
-126
lines changed

compiler/rustc_attr_data_structures/src/attributes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ pub enum AttributeKind {
233233
ProcMacro,
234234
ProcMacroAttribute,
235235
ProcMacroDerive,
236-
Repr(ThinVec<ReprAttr>),
236+
Repr(ThinVec<(ReprAttr, Span)>),
237237
Stability {
238238
stability: Stability,
239239
/// Span of the `#[stable(...)]` or `#[unstable(...)]` attribute

compiler/rustc_attr_parsing/messages.ftl

+7
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ attr_parsing_invalid_repr_hint_no_paren =
6666
attr_parsing_invalid_repr_hint_no_value =
6767
invalid representation hint: `{$name}` does not take a value
6868
69+
attr_parsing_unrecognized_repr_hint =
70+
unrecognized representation hint
71+
.help = valid reprs are `Rust` (default), `C`, `align`, `packed`, `transparent`, `simd`, `i8`, `u8`, `i16`, `u16`, `i32`, `u32`, `i64`, `u64`, `i128`, `u128`, `isize`, `usize`
72+
6973
attr_parsing_invalid_since =
7074
'since' must be a Rust version number, such as "1.31.0"
7175
@@ -128,3 +132,6 @@ attr_parsing_unused_multiple =
128132
multiple `{$name}` attributes
129133
.suggestion = remove this attribute
130134
.note = attribute also specified here
135+
136+
attr_parsing_repr_ident =
137+
meta item in `repr` must be an identifier

compiler/rustc_attr_parsing/src/attributes/repr.rs

+6-12
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,23 @@ use crate::session_diagnostics::IncorrectReprFormatGenericCause;
2424
pub(crate) struct ReprGroup;
2525

2626
impl CombineAttributeGroup for ReprGroup {
27-
type Item = ReprAttr;
27+
type Item = (ReprAttr, Span);
2828
const PATH: &'static [rustc_span::Symbol] = &[sym::repr];
29-
const CONVERT: ConvertFn<ReprAttr> = AttributeKind::Repr;
29+
const CONVERT: ConvertFn<Self::Item> = AttributeKind::Repr;
3030

3131
fn extend<'a>(
3232
cx: &'a AttributeAcceptContext<'a>,
3333
args: &'a GenericArgParser<'a, rustc_ast::Expr>,
34-
) -> impl IntoIterator<Item = ReprAttr> + 'a {
34+
) -> impl IntoIterator<Item = Self::Item> + 'a {
3535
let mut reprs = Vec::new();
3636

3737
let Some(list) = args.list() else {
3838
return reprs;
3939
};
4040

4141
for param in list.mixed() {
42-
reprs.extend(param.meta_item().and_then(|mi| parse_repr(cx, &mi)));
42+
let span = param.span();
43+
reprs.extend(param.meta_item().and_then(|mi| parse_repr(cx, &mi)).map(|r| (r, span)));
4344
}
4445

4546
reprs
@@ -266,15 +267,8 @@ fn parse_simple_repr(
266267
}
267268
}
268269

269-
// Not a word we recognize. This will be caught and reported by
270-
// the `check_mod_attrs` pass, but this pass doesn't always run
271-
// (e.g. if we only pretty-print the source), so we have to gate
272-
// the `span_delayed_bug` call as follows:
273-
// TODO: remove this in favor of just reporting the error here if we can...
274270
fn completely_unknown(cx: &AttributeAcceptContext<'_>, param_span: Span) {
275-
if cx.sess().opts.pretty.map_or(true, |pp| pp.needs_analysis()) {
276-
cx.dcx().span_delayed_bug(param_span, "unrecognized representation hint");
277-
}
271+
cx.dcx().emit_err(session_diagnostics::UnrecognizedReprHint { span: param_span });
278272
}
279273

280274
fn int_type_of_word(s: Symbol) -> Option<IntType> {

compiler/rustc_attr_parsing/src/session_diagnostics.rs

+15
Original file line numberDiff line numberDiff line change
@@ -458,3 +458,18 @@ pub(crate) struct EmptyConfusables {
458458
#[primary_span]
459459
pub span: Span,
460460
}
461+
462+
#[derive(Diagnostic)]
463+
#[diag(attr_parsing_repr_ident, code = E0565)]
464+
pub(crate) struct ReprIdent {
465+
#[primary_span]
466+
pub span: Span,
467+
}
468+
469+
#[derive(Diagnostic)]
470+
#[diag(attr_parsing_unrecognized_repr_hint, code = E0552)]
471+
#[help]
472+
pub(crate) struct UnrecognizedReprHint {
473+
#[primary_span]
474+
pub span: Span,
475+
}

compiler/rustc_builtin_macros/src/deriving/coerce_pointee.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub(crate) fn expand_deriving_coerce_pointee(
3535
{
3636
let is_transparent = matches!(
3737
AttributeParseContext::parse_limited(cx.sess, &aitem.attrs, sym::repr, span),
38-
Some(Attribute::Parsed(AttributeKind::Repr(r))) if r.contains(&ReprTransparent)
38+
Some(Attribute::Parsed(AttributeKind::Repr(r))) if r.iter().any(|(r, _)| r == &ReprTransparent)
3939
);
4040

4141
if !is_transparent {

compiler/rustc_builtin_macros/src/deriving/generic/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ impl<'a> TraitDef<'a> {
482482
Annotatable::Item(item) => {
483483
let is_packed = matches!(
484484
AttributeParseContext::parse_limited(cx.sess, &item.attrs, sym::repr, item.span),
485-
Some(Attribute::Parsed(AttributeKind::Repr(r))) if r.iter().any(|x| matches!(x, ReprPacked(..)))
485+
Some(Attribute::Parsed(AttributeKind::Repr(r))) if r.iter().any(|(x, _)| matches!(x, ReprPacked(..)))
486486
);
487487

488488
let newitem = match &item.kind {

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
107107
AttributeKind::Repr(reprs) => {
108108
codegen_fn_attrs.alignment = reprs
109109
.iter()
110-
.find_map(|r| if let ReprAlign(x) = r { Some(*x) } else { None });
110+
.find_map(|(r, _)| if let ReprAlign(x) = r { Some(*x) } else { None });
111111
}
112112

113113
_ => {}

compiler/rustc_hir/src/hir.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1104,7 +1104,9 @@ impl AttributeExt for Attribute {
11041104
fn span(&self) -> Span {
11051105
match &self {
11061106
Attribute::Unparsed(u) => u.span,
1107-
_ => panic!(),
1107+
// FIXME: should not be needed anymore when all attrs are parsed
1108+
Attribute::Parsed(AttributeKind::Deprecation { span, .. }) => *span,
1109+
a => panic!("can't get the span of an arbitrary parsed attribute: {a:?}"),
11081110
}
11091111
}
11101112

compiler/rustc_hir_analysis/src/check/check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1253,7 +1253,7 @@ pub(super) fn check_packed(tcx: TyCtxt<'_>, sp: Span, def: ty::AdtDef<'_>) {
12531253
if let Some(reprs) =
12541254
attr::find_attr!(tcx.get_all_attrs(def.did()), AttributeKind::Repr(r) => r)
12551255
{
1256-
for r in reprs {
1256+
for (r, _) in reprs {
12571257
if let ReprPacked(pack) = r
12581258
&& let Some(repr_pack) = repr.pack
12591259
&& pack != &repr_pack

compiler/rustc_lint/src/nonstandard_style.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ impl EarlyLintPass for NonCamelCaseTypes {
164164
fn check_item(&mut self, cx: &EarlyContext<'_>, it: &ast::Item) {
165165
let has_repr_c = matches!(
166166
AttributeParseContext::parse_limited(cx.sess(), &it.attrs, sym::repr, it.span),
167-
Some(Attribute::Parsed(AttributeKind::Repr(r))) if r.contains(&ReprAttr::ReprC)
167+
Some(Attribute::Parsed(AttributeKind::Repr(r))) if r.iter().any(|(r, _)| r == &ReprAttr::ReprC)
168168
);
169169

170170
if has_repr_c {

compiler/rustc_middle/src/ty/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1500,7 +1500,7 @@ impl<'tcx> TyCtxt<'tcx> {
15001500

15011501
if let Some(reprs) = attr::find_attr!(self.get_all_attrs(did), AttributeKind::Repr(r) => r)
15021502
{
1503-
for r in reprs {
1503+
for (r, _) in reprs {
15041504
flags.insert(match *r {
15051505
attr::ReprRust => ReprFlags::empty(),
15061506
attr::ReprC => ReprFlags::IS_C,

compiler/rustc_passes/messages.ftl

-7
Original file line numberDiff line numberDiff line change
@@ -640,9 +640,6 @@ passes_repr_align_greater_than_target_max =
640640
passes_repr_conflicting =
641641
conflicting representation hints
642642
643-
passes_repr_ident =
644-
meta item in `repr` must be an identifier
645-
646643
passes_rustc_allow_const_fn_unstable =
647644
attribute should be applied to `const fn`
648645
.label = not a `const fn`
@@ -775,10 +772,6 @@ passes_unreachable_due_to_uninhabited = unreachable {$descr}
775772
passes_unrecognized_field =
776773
unrecognized field name `{$name}`
777774
778-
passes_unrecognized_repr_hint =
779-
unrecognized representation hint
780-
.help = valid reprs are `Rust` (default), `C`, `align`, `packed`, `transparent`, `simd`, `i8`, `u8`, `i16`, `u16`, `i32`, `u32`, `i64`, `u64`, `i128`, `u128`, `isize`, `usize`
781-
782775
passes_unstable_attr_for_already_stable_feature =
783776
can't mark as unstable using an already stable feature
784777
.label = this feature is already stable

0 commit comments

Comments
 (0)