Skip to content

Commit 0241292

Browse files
committed
take attr style into account in diagnostics
1 parent 2c1ac85 commit 0241292

File tree

13 files changed

+53
-37
lines changed

13 files changed

+53
-37
lines changed

compiler/rustc_attr_parsing/src/attributes/inline.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ impl<S: Stage> SingleAttributeParser<S> for InlineParser {
4747
}
4848
}
4949
ArgParser::NameValue(_) => {
50-
let suggestions =
51-
<Self as SingleAttributeParser<S>>::TEMPLATE.suggestions(false, "inline");
50+
let suggestions = <Self as SingleAttributeParser<S>>::TEMPLATE
51+
.suggestions(cx.attr_style, "inline");
5252
let span = cx.attr_span;
5353
cx.emit_lint(AttributeLintKind::IllFormedAttributeInput { suggestions }, span);
5454
return None;

compiler/rustc_attr_parsing/src/attributes/macro_attrs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ impl<S: Stage> AttributeParser<S> for MacroUseParser {
9999
}
100100
}
101101
ArgParser::NameValue(_) => {
102-
let suggestions = MACRO_USE_TEMPLATE.suggestions(false, sym::macro_use);
102+
let suggestions = MACRO_USE_TEMPLATE.suggestions(cx.attr_style, sym::macro_use);
103103
cx.emit_err(session_diagnostics::IllFormedAttributeInputLint {
104104
num_suggestions: suggestions.len(),
105105
suggestions: DiagArgValue::StrListSepByAnd(

compiler/rustc_attr_parsing/src/attributes/must_use.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ impl<S: Stage> SingleAttributeParser<S> for MustUseParser {
3535
Some(value_str)
3636
}
3737
ArgParser::List(_) => {
38-
let suggestions =
39-
<Self as SingleAttributeParser<S>>::TEMPLATE.suggestions(false, "must_use");
38+
let suggestions = <Self as SingleAttributeParser<S>>::TEMPLATE
39+
.suggestions(cx.attr_style, "must_use");
4040
cx.emit_err(session_diagnostics::IllFormedAttributeInputLint {
4141
num_suggestions: suggestions.len(),
4242
suggestions: DiagArgValue::StrListSepByAnd(

compiler/rustc_attr_parsing/src/attributes/test_attrs.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ impl<S: Stage> SingleAttributeParser<S> for IgnoreParser {
2626
ArgParser::NameValue(name_value) => {
2727
let Some(str_value) = name_value.value_as_str() else {
2828
let suggestions = <Self as SingleAttributeParser<S>>::TEMPLATE
29-
.suggestions(false, "ignore");
29+
.suggestions(cx.attr_style, "ignore");
3030
let span = cx.attr_span;
3131
cx.emit_lint(
3232
AttributeLintKind::IllFormedAttributeInput { suggestions },
@@ -37,8 +37,8 @@ impl<S: Stage> SingleAttributeParser<S> for IgnoreParser {
3737
Some(str_value)
3838
}
3939
ArgParser::List(_) => {
40-
let suggestions =
41-
<Self as SingleAttributeParser<S>>::TEMPLATE.suggestions(false, "ignore");
40+
let suggestions = <Self as SingleAttributeParser<S>>::TEMPLATE
41+
.suggestions(cx.attr_style, "ignore");
4242
let span = cx.attr_span;
4343
cx.emit_lint(AttributeLintKind::IllFormedAttributeInput { suggestions }, span);
4444
return None;

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::ops::{Deref, DerefMut};
44
use std::sync::LazyLock;
55

66
use private::Sealed;
7-
use rustc_ast::{self as ast, LitKind, MetaItemLit, NodeId};
7+
use rustc_ast::{self as ast, AttrStyle, LitKind, MetaItemLit, NodeId};
88
use rustc_errors::{DiagCtxtHandle, Diagnostic};
99
use rustc_feature::{AttributeTemplate, Features};
1010
use rustc_hir::attrs::AttributeKind;
@@ -304,6 +304,7 @@ pub struct AcceptContext<'f, 'sess, S: Stage> {
304304
/// The span of the attribute currently being parsed
305305
pub(crate) attr_span: Span,
306306

307+
pub(crate) attr_style: AttrStyle,
307308
/// The expected structure of the attribute.
308309
///
309310
/// Used in reporting errors to give a hint to users what the attribute *should* look like.
@@ -385,6 +386,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
385386
i.kind.is_bytestr().then(|| self.sess().source_map().start_point(i.span))
386387
}),
387388
},
389+
attr_style: self.attr_style,
388390
})
389391
}
390392

@@ -395,6 +397,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
395397
template: self.template.clone(),
396398
attribute: self.attr_path.clone(),
397399
reason: AttributeParseErrorReason::ExpectedIntegerLiteral,
400+
attr_style: self.attr_style,
398401
})
399402
}
400403

@@ -405,6 +408,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
405408
template: self.template.clone(),
406409
attribute: self.attr_path.clone(),
407410
reason: AttributeParseErrorReason::ExpectedList,
411+
attr_style: self.attr_style,
408412
})
409413
}
410414

@@ -415,6 +419,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
415419
template: self.template.clone(),
416420
attribute: self.attr_path.clone(),
417421
reason: AttributeParseErrorReason::ExpectedNoArgs,
422+
attr_style: self.attr_style,
418423
})
419424
}
420425

@@ -426,6 +431,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
426431
template: self.template.clone(),
427432
attribute: self.attr_path.clone(),
428433
reason: AttributeParseErrorReason::ExpectedIdentifier,
434+
attr_style: self.attr_style,
429435
})
430436
}
431437

@@ -438,6 +444,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
438444
template: self.template.clone(),
439445
attribute: self.attr_path.clone(),
440446
reason: AttributeParseErrorReason::ExpectedNameValue(name),
447+
attr_style: self.attr_style,
441448
})
442449
}
443450

@@ -449,6 +456,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
449456
template: self.template.clone(),
450457
attribute: self.attr_path.clone(),
451458
reason: AttributeParseErrorReason::DuplicateKey(key),
459+
attr_style: self.attr_style,
452460
})
453461
}
454462

@@ -461,6 +469,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
461469
template: self.template.clone(),
462470
attribute: self.attr_path.clone(),
463471
reason: AttributeParseErrorReason::UnexpectedLiteral,
472+
attr_style: self.attr_style,
464473
})
465474
}
466475

@@ -471,6 +480,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
471480
template: self.template.clone(),
472481
attribute: self.attr_path.clone(),
473482
reason: AttributeParseErrorReason::ExpectedSingleArgument,
483+
attr_style: self.attr_style,
474484
})
475485
}
476486

@@ -481,6 +491,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
481491
template: self.template.clone(),
482492
attribute: self.attr_path.clone(),
483493
reason: AttributeParseErrorReason::ExpectedAtLeastOneArgument,
494+
attr_style: self.attr_style,
484495
})
485496
}
486497

@@ -499,6 +510,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
499510
strings: false,
500511
list: false,
501512
},
513+
attr_style: self.attr_style,
502514
})
503515
}
504516

@@ -517,6 +529,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
517529
strings: false,
518530
list: true,
519531
},
532+
attr_style: self.attr_style,
520533
})
521534
}
522535

@@ -535,6 +548,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
535548
strings: true,
536549
list: false,
537550
},
551+
attr_style: self.attr_style,
538552
})
539553
}
540554

@@ -734,6 +748,7 @@ impl<'sess> AttributeParser<'sess, Early> {
734748
},
735749
},
736750
attr_span: attr.span,
751+
attr_style: attr.style,
737752
template,
738753
attr_path: path.get_attribute_path(),
739754
};
@@ -843,6 +858,7 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
843858
emit_lint: &mut emit_lint,
844859
},
845860
attr_span: lower_span(attr.span),
861+
attr_style: attr.style,
846862
template: &accept.template,
847863
attr_path: path.get_attribute_path(),
848864
};

compiler/rustc_attr_parsing/src/session_diagnostics.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::num::IntErrorKind;
22

3-
use rustc_ast as ast;
3+
use rustc_ast::{self as ast, AttrStyle};
44
use rustc_errors::codes::*;
55
use rustc_errors::{
66
Applicability, Diag, DiagArgValue, DiagCtxtHandle, Diagnostic, EmissionGuarantee, Level,
@@ -556,6 +556,7 @@ pub(crate) enum AttributeParseErrorReason {
556556
pub(crate) struct AttributeParseError {
557557
pub(crate) span: Span,
558558
pub(crate) attr_span: Span,
559+
pub(crate) attr_style: AttrStyle,
559560
pub(crate) template: AttributeTemplate,
560561
pub(crate) attribute: AttrPath,
561562
pub(crate) reason: AttributeParseErrorReason,
@@ -694,7 +695,8 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for AttributeParseError {
694695
if let Some(link) = self.template.docs {
695696
diag.note(format!("for more information, visit <{link}>"));
696697
}
697-
let suggestions = self.template.suggestions(false, &name);
698+
let suggestions = self.template.suggestions(self.attr_style, &name);
699+
698700
diag.span_suggestions(
699701
self.attr_span,
700702
if suggestions.len() == 1 {

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use AttributeDuplicates::*;
66
use AttributeGate::*;
77
use AttributeType::*;
88
use rustc_data_structures::fx::FxHashMap;
9+
use rustc_hir::AttrStyle;
910
use rustc_hir::attrs::EncodeCrossCrate;
1011
use rustc_span::edition::Edition;
1112
use rustc_span::{Symbol, sym};
@@ -132,9 +133,12 @@ pub struct AttributeTemplate {
132133
}
133134

134135
impl AttributeTemplate {
135-
pub fn suggestions(&self, inner: bool, name: impl std::fmt::Display) -> Vec<String> {
136+
pub fn suggestions(&self, style: AttrStyle, name: impl std::fmt::Display) -> Vec<String> {
136137
let mut suggestions = vec![];
137-
let inner = if inner { "!" } else { "" };
138+
let inner = match style {
139+
AttrStyle::Outer => "",
140+
AttrStyle::Inner => "!",
141+
};
138142
if self.word {
139143
suggestions.push(format!("#{inner}[{name}]"));
140144
}

tests/ui/attributes/lint_on_root.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// NOTE: this used to panic in debug builds (by a sanity assertion)
22
// and not emit any lint on release builds. See https://github.com/rust-lang/rust/issues/142891.
33
#![inline = ""]
4-
//~^ ERROR: valid forms for the attribute are `#[inline(always)]`, `#[inline(never)]`, and `#[inline]` [ill_formed_attribute_input]
4+
//~^ ERROR: valid forms for the attribute are `#![inline(always)]`, `#![inline(never)]`, and `#![inline]` [ill_formed_attribute_input]
55
//~| WARN this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
66

77
fn main() {}

tests/ui/attributes/lint_on_root.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: valid forms for the attribute are `#[inline(always)]`, `#[inline(never)]`, and `#[inline]`
1+
error: valid forms for the attribute are `#![inline(always)]`, `#![inline(never)]`, and `#![inline]`
22
--> $DIR/lint_on_root.rs:3:1
33
|
44
LL | #![inline = ""]
@@ -11,7 +11,7 @@ LL | #![inline = ""]
1111
error: aborting due to 1 previous error
1212

1313
Future incompatibility report: Future breakage diagnostic:
14-
error: valid forms for the attribute are `#[inline(always)]`, `#[inline(never)]`, and `#[inline]`
14+
error: valid forms for the attribute are `#![inline(always)]`, `#![inline(never)]`, and `#![inline]`
1515
--> $DIR/lint_on_root.rs:3:1
1616
|
1717
LL | #![inline = ""]

tests/ui/attributes/malformed-reprs.stderr

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,14 @@ LL | #![repr]
77
= note: for more information, visit <https://doc.rust-lang.org/reference/type-layout.html#representations>
88
help: try changing it to one of the following valid forms of the attribute
99
|
10-
LL - #![repr]
11-
LL + #[repr(<integer type>)]
12-
|
13-
LL - #![repr]
14-
LL + #[repr(C)]
15-
|
16-
LL - #![repr]
17-
LL + #[repr(Rust)]
18-
|
19-
LL - #![repr]
20-
LL + #[repr(align(...))]
21-
|
10+
LL | #![repr(<integer type>)]
11+
| ++++++++++++++++
12+
LL | #![repr(C)]
13+
| +++
14+
LL | #![repr(Rust)]
15+
| ++++++
16+
LL | #![repr(align(...))]
17+
| ++++++++++++
2218
= and 2 other candidates
2319

2420
error[E0589]: invalid `repr(align)` attribute: not a power of two

0 commit comments

Comments
 (0)