Skip to content

Commit c72dc2b

Browse files
Clean up extend function of ReprParser
1 parent 525723d commit c72dc2b

3 files changed

Lines changed: 20 additions & 28 deletions

File tree

compiler/rustc_attr_parsing/src/attributes/repr.rs

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@ use crate::session_diagnostics::{self, IncorrectReprFormatGenericCause};
77

88
/// Parse #[repr(...)] forms.
99
///
10-
/// Valid repr contents: any of the primitive integral type names (see
11-
/// `int_type_of_word`, below) to specify enum discriminant type; `C`, to use
12-
/// the same discriminant size that the corresponding C enum would or C
13-
/// structure layout, `packed` to remove padding, and `transparent` to delegate representation
14-
/// concerns to the only non-ZST field.
15-
// FIXME(jdonszelmann): is a vec the right representation here even? isn't it just a struct?
10+
/// Valid repr contents:
11+
/// * any of the primitive integral type names to specify enum discriminant type
12+
/// * `Rust`, to use the default `Rust` layout of the type
13+
/// * `C`, to use the same layout for the type that C would use
14+
/// * `align(...)`, to change the alignment requirements of the type
15+
/// * `packed`, to remove padding
16+
/// * `transparent`, to delegate representation concerns to the only non-ZST field.
1617
pub(crate) struct ReprParser;
1718

1819
impl CombineAttributeParser for ReprParser {
1920
type Item = (ReprAttr, Span);
2021
const PATH: &[Symbol] = &[sym::repr];
2122
const CONVERT: ConvertFn<Self::Item> =
2223
|items, first_span| AttributeKind::Repr { reprs: items, first_span };
23-
// FIXME(jdonszelmann): never used
2424
const TEMPLATE: AttributeTemplate = template!(
2525
List: &["C", "Rust", "transparent", "align(...)", "packed(...)", "<integer type>"],
2626
"https://doc.rust-lang.org/reference/type-layout.html#representations"
@@ -30,29 +30,24 @@ impl CombineAttributeParser for ReprParser {
3030
cx: &mut AcceptContext<'_, '_>,
3131
args: &ArgParser,
3232
) -> impl IntoIterator<Item = Self::Item> {
33-
let mut reprs = Vec::new();
34-
3533
let Some(list) = cx.expect_list(args, cx.attr_span) else {
36-
return reprs;
34+
return vec![];
3735
};
3836

3937
if list.is_empty() {
4038
let attr_span = cx.attr_span;
4139
cx.adcx().warn_empty_attribute(attr_span);
42-
return reprs;
40+
return vec![];
4341
}
4442

43+
let mut reprs = Vec::new();
4544
for param in list.mixed() {
46-
if let Some(_) = param.as_lit() {
47-
cx.emit_err(session_diagnostics::ReprIdent { span: cx.attr_span });
45+
let Some(item) = param.meta_item() else {
46+
cx.adcx().expected_identifier(param.span());
4847
continue;
49-
}
50-
51-
reprs.extend(
52-
param.meta_item().and_then(|mi| parse_repr(cx, &mi)).map(|r| (r, param.span())),
53-
);
48+
};
49+
reprs.extend(parse_repr(cx, &item).map(|r| (r, param.span())));
5450
}
55-
5651
reprs
5752
}
5853

compiler/rustc_attr_parsing/src/session_diagnostics.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -484,13 +484,6 @@ pub(crate) struct InvalidAlignmentValue {
484484
pub error_part: String,
485485
}
486486

487-
#[derive(Diagnostic)]
488-
#[diag("meta item in `repr` must be an identifier", code = E0565)]
489-
pub(crate) struct ReprIdent {
490-
#[primary_span]
491-
pub span: Span,
492-
}
493-
494487
#[derive(Diagnostic)]
495488
#[diag("unrecognized representation hint", code = E0552)]
496489
#[help(

tests/ui/error-codes/E0565.stderr

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
error[E0565]: meta item in `repr` must be an identifier
1+
error[E0565]: malformed `repr` attribute input
22
--> $DIR/E0565.rs:2:1
33
|
44
LL | #[repr("C")]
5-
| ^^^^^^^^^^^^
5+
| ^^^^^^^---^^
6+
| |
7+
| expected a valid identifier here
8+
|
9+
= note: for more information, visit <https://doc.rust-lang.org/reference/type-layout.html#representations>
610

711
error: aborting due to 1 previous error
812

0 commit comments

Comments
 (0)