Skip to content

Commit 65316eb

Browse files
committed
take attr style into account in diagnostics
1 parent a6620a4 commit 65316eb

File tree

6 files changed

+28
-13
lines changed

6 files changed

+28
-13
lines changed

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 == AttrStyle::Inner, &name);
694695
diag.span_suggestions(
695696
self.attr_span,
696697
if suggestions.len() == 1 {

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

tests/ui/coverage-attr/name-value.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ LL | #![coverage = "off"]
2222
help: try changing it to one of the following valid forms of the attribute
2323
|
2424
LL - #![coverage = "off"]
25-
LL + #[coverage(off)]
25+
LL + #![coverage(off)]
2626
|
2727
LL - #![coverage = "off"]
28-
LL + #[coverage(on)]
28+
LL + #![coverage(on)]
2929
|
3030

3131
error[E0539]: malformed `coverage` attribute input

tests/ui/coverage-attr/word-only.stderr

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@ LL | #![coverage]
1919
|
2020
help: try changing it to one of the following valid forms of the attribute
2121
|
22-
LL - #![coverage]
23-
LL + #[coverage(off)]
24-
|
25-
LL - #![coverage]
26-
LL + #[coverage(on)]
27-
|
22+
LL | #![coverage(off)]
23+
| +++++
24+
LL | #![coverage(on)]
25+
| ++++
2826

2927
error[E0539]: malformed `coverage` attribute input
3028
--> $DIR/word-only.rs:21:1

tests/ui/resolve/path-attr-in-const-block.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | #![path = foo!()]
1111
| ^^^^^^^^^^------^
1212
| | |
1313
| | expected a string literal here
14-
| help: must be of the form: `#[path = "file"]`
14+
| help: must be of the form: `#![path = "file"]`
1515

1616
error: aborting due to 2 previous errors
1717

0 commit comments

Comments
 (0)