Skip to content

Commit 68d388f

Browse files
committed
take attr style into account in diagnostics
1 parent a153133 commit 68d388f

File tree

13 files changed

+45
-26
lines changed

13 files changed

+45
-26
lines changed

compiler/rustc_attr_parsing/src/attributes/inline.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ impl<S: Stage> SingleAttributeParser<S> for InlineParser {
4343
}
4444
}
4545
ArgParser::NameValue(_) => {
46-
let suggestions =
47-
<Self as SingleAttributeParser<S>>::TEMPLATE.suggestions(false, "inline");
46+
let suggestions = <Self as SingleAttributeParser<S>>::TEMPLATE
47+
.suggestions(cx.attr_style, "inline");
4848
let span = cx.attr_span;
4949
cx.emit_lint(AttributeLintKind::IllFormedAttributeInput { suggestions }, span);
5050
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
@@ -96,7 +96,7 @@ impl<S: Stage> AttributeParser<S> for MacroUseParser {
9696
}
9797
}
9898
ArgParser::NameValue(_) => {
99-
let suggestions = MACRO_USE_TEMPLATE.suggestions(false, sym::macro_use);
99+
let suggestions = MACRO_USE_TEMPLATE.suggestions(cx.attr_style, sym::macro_use);
100100
cx.emit_err(session_diagnostics::IllFormedAttributeInputLint {
101101
num_suggestions: suggestions.len(),
102102
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
@@ -32,8 +32,8 @@ impl<S: Stage> SingleAttributeParser<S> for MustUseParser {
3232
Some(value_str)
3333
}
3434
ArgParser::List(_) => {
35-
let suggestions =
36-
<Self as SingleAttributeParser<S>>::TEMPLATE.suggestions(false, "must_use");
35+
let suggestions = <Self as SingleAttributeParser<S>>::TEMPLATE
36+
.suggestions(cx.attr_style, "must_use");
3737
cx.emit_err(session_diagnostics::IllFormedAttributeInputLint {
3838
num_suggestions: suggestions.len(),
3939
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
@@ -23,7 +23,7 @@ impl<S: Stage> SingleAttributeParser<S> for IgnoreParser {
2323
ArgParser::NameValue(name_value) => {
2424
let Some(str_value) = name_value.value_as_str() else {
2525
let suggestions = <Self as SingleAttributeParser<S>>::TEMPLATE
26-
.suggestions(false, "ignore");
26+
.suggestions(cx.attr_style, "ignore");
2727
let span = cx.attr_span;
2828
cx.emit_lint(
2929
AttributeLintKind::IllFormedAttributeInput { suggestions },
@@ -34,8 +34,8 @@ impl<S: Stage> SingleAttributeParser<S> for IgnoreParser {
3434
Some(str_value)
3535
}
3636
ArgParser::List(_) => {
37-
let suggestions =
38-
<Self as SingleAttributeParser<S>>::TEMPLATE.suggestions(false, "ignore");
37+
let suggestions = <Self as SingleAttributeParser<S>>::TEMPLATE
38+
.suggestions(cx.attr_style, "ignore");
3939
let span = cx.attr_span;
4040
cx.emit_lint(AttributeLintKind::IllFormedAttributeInput { suggestions }, span);
4141
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;
@@ -301,6 +301,7 @@ pub struct AcceptContext<'f, 'sess, S: Stage> {
301301
/// The span of the attribute currently being parsed
302302
pub(crate) attr_span: Span,
303303

304+
pub(crate) attr_style: AttrStyle,
304305
/// The expected structure of the attribute.
305306
///
306307
/// Used in reporting errors to give a hint to users what the attribute *should* look like.
@@ -382,6 +383,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
382383
i.kind.is_bytestr().then(|| self.sess().source_map().start_point(i.span))
383384
}),
384385
},
386+
attr_style: self.attr_style,
385387
})
386388
}
387389

@@ -392,6 +394,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
392394
template: self.template.clone(),
393395
attribute: self.attr_path.clone(),
394396
reason: AttributeParseErrorReason::ExpectedIntegerLiteral,
397+
attr_style: self.attr_style,
395398
})
396399
}
397400

@@ -402,6 +405,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
402405
template: self.template.clone(),
403406
attribute: self.attr_path.clone(),
404407
reason: AttributeParseErrorReason::ExpectedList,
408+
attr_style: self.attr_style,
405409
})
406410
}
407411

@@ -412,6 +416,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
412416
template: self.template.clone(),
413417
attribute: self.attr_path.clone(),
414418
reason: AttributeParseErrorReason::ExpectedNoArgs,
419+
attr_style: self.attr_style,
415420
})
416421
}
417422

@@ -423,6 +428,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
423428
template: self.template.clone(),
424429
attribute: self.attr_path.clone(),
425430
reason: AttributeParseErrorReason::ExpectedIdentifier,
431+
attr_style: self.attr_style,
426432
})
427433
}
428434

@@ -435,6 +441,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
435441
template: self.template.clone(),
436442
attribute: self.attr_path.clone(),
437443
reason: AttributeParseErrorReason::ExpectedNameValue(name),
444+
attr_style: self.attr_style,
438445
})
439446
}
440447

@@ -446,6 +453,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
446453
template: self.template.clone(),
447454
attribute: self.attr_path.clone(),
448455
reason: AttributeParseErrorReason::DuplicateKey(key),
456+
attr_style: self.attr_style,
449457
})
450458
}
451459

@@ -458,6 +466,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
458466
template: self.template.clone(),
459467
attribute: self.attr_path.clone(),
460468
reason: AttributeParseErrorReason::UnexpectedLiteral,
469+
attr_style: self.attr_style,
461470
})
462471
}
463472

@@ -468,6 +477,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
468477
template: self.template.clone(),
469478
attribute: self.attr_path.clone(),
470479
reason: AttributeParseErrorReason::ExpectedSingleArgument,
480+
attr_style: self.attr_style,
471481
})
472482
}
473483

@@ -478,6 +488,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
478488
template: self.template.clone(),
479489
attribute: self.attr_path.clone(),
480490
reason: AttributeParseErrorReason::ExpectedAtLeastOneArgument,
491+
attr_style: self.attr_style,
481492
})
482493
}
483494

@@ -496,6 +507,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
496507
strings: false,
497508
list: false,
498509
},
510+
attr_style: self.attr_style,
499511
})
500512
}
501513

@@ -514,6 +526,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
514526
strings: false,
515527
list: true,
516528
},
529+
attr_style: self.attr_style,
517530
})
518531
}
519532

@@ -532,6 +545,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
532545
strings: true,
533546
list: false,
534547
},
548+
attr_style: self.attr_style,
535549
})
536550
}
537551

@@ -731,6 +745,7 @@ impl<'sess> AttributeParser<'sess, Early> {
731745
},
732746
},
733747
attr_span: attr.span,
748+
attr_style: attr.style,
734749
template,
735750
attr_path: path.get_attribute_path(),
736751
};
@@ -840,6 +855,7 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
840855
emit_lint: &mut emit_lint,
841856
},
842857
attr_span: lower_span(attr.span),
858+
attr_style: attr.style,
843859
template: &accept.template,
844860
attr_path: path.get_attribute_path(),
845861
};

compiler/rustc_attr_parsing/src/session_diagnostics.rs

Lines changed: 3 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,
@@ -555,6 +555,7 @@ pub(crate) enum AttributeParseErrorReason {
555555
pub(crate) struct AttributeParseError {
556556
pub(crate) span: Span,
557557
pub(crate) attr_span: Span,
558+
pub(crate) attr_style: AttrStyle,
558559
pub(crate) template: AttributeTemplate,
559560
pub(crate) attribute: AttrPath,
560561
pub(crate) reason: AttributeParseErrorReason,
@@ -690,7 +691,7 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for AttributeParseError {
690691
}
691692
}
692693

693-
let suggestions = self.template.suggestions(false, &name);
694+
let suggestions = self.template.suggestions(self.attr_style, &name);
694695
diag.span_suggestions(
695696
self.attr_span,
696697
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};
@@ -130,9 +131,12 @@ pub struct AttributeTemplate {
130131
}
131132

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

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|never)]` and `#[inline]`
4+
//~^ ERROR valid forms for the attribute are `#![inline(always|never)]` and `#![inline]`
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|never)]` and `#[inline]`
1+
error: valid forms for the attribute are `#![inline(always|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|never)]` and `#[inline]`
14+
error: valid forms for the attribute are `#![inline(always|never)]` and `#![inline]`
1515
--> $DIR/lint_on_root.rs:3:1
1616
|
1717
LL | #![inline = ""]

tests/ui/attributes/malformed-reprs.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | #![repr]
55
| ^^^^^^^^
66
| |
77
| expected this to be a list
8-
| help: must be of the form: `#[repr(C | Rust | align(...) | packed(...) | <integer type> | transparent)]`
8+
| help: must be of the form: `#![repr(C | Rust | align(...) | packed(...) | <integer type> | transparent)]`
99

1010
error[E0589]: invalid `repr(align)` attribute: not a power of two
1111
--> $DIR/malformed-reprs.rs:9:14

0 commit comments

Comments
 (0)