Skip to content

Commit 961a2cd

Browse files
committed
Add a helper expect_no_args method
1 parent b431b40 commit 961a2cd

File tree

7 files changed

+34
-14
lines changed

7 files changed

+34
-14
lines changed

compiler/rustc_attr_parsing/src/attributes/lint_helpers.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_attr_data_structures::AttributeKind;
22
use rustc_feature::{AttributeTemplate, template};
3-
use rustc_span::{Symbol, sym};
3+
use rustc_span::{ErrorGuaranteed, Symbol, sym};
44

55
use crate::attributes::{AttributeOrder, OnDuplicate, SingleAttributeParser};
66
use crate::context::{AcceptContext, Stage};
@@ -14,8 +14,8 @@ impl<S: Stage> SingleAttributeParser<S> for AsPtrParser {
1414
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
1515
const TEMPLATE: AttributeTemplate = template!(Word);
1616

17-
fn convert(cx: &mut AcceptContext<'_, '_, S>, _args: &ArgParser<'_>) -> Option<AttributeKind> {
18-
// FIXME: check that there's no args (this is currently checked elsewhere)
17+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
18+
let _: Result<(), ErrorGuaranteed> = cx.expect_no_args(args);
1919
Some(AttributeKind::AsPtr(cx.attr_span))
2020
}
2121
}

compiler/rustc_attr_parsing/src/attributes/resolution.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use core::mem;
22

33
use rustc_attr_data_structures::AttributeKind;
44
use rustc_feature::{AttributeTemplate, template};
5-
use rustc_span::{Symbol, sym};
5+
use rustc_span::{ErrorGuaranteed, Symbol, sym};
66

77
use crate::attributes::{AttributeOrder, OnDuplicate, SingleAttributeParser};
88
use crate::context::{AcceptContext, Stage};
@@ -33,13 +33,7 @@ impl<S: Stage> SingleAttributeParser<S> for SkipDuringMethodDispatchParser {
3333
cx.unexpected_literal(arg.span());
3434
continue;
3535
};
36-
if let Some(span) = match arg.args() {
37-
ArgParser::NoArgs => None,
38-
ArgParser::List(args) => Some(args.span),
39-
ArgParser::NameValue(args) => Some(args.eq_span.to(args.value_span)),
40-
} {
41-
cx.unexpected_literal(span);
42-
}
36+
let _: Result<(), ErrorGuaranteed> = cx.expect_no_args(arg.args());
4337
let path = arg.path();
4438
let (key, skip): (Symbol, &mut bool) = match path.word_sym() {
4539
Some(key @ sym::array) => (key, &mut array),

compiler/rustc_attr_parsing/src/attributes/stability.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ impl<S: Stage> SingleAttributeParser<S> for ConstStabilityIndirectParser {
139139
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Ignore;
140140
const TEMPLATE: AttributeTemplate = template!(Word);
141141

142-
fn convert(_cx: &mut AcceptContext<'_, '_, S>, _args: &ArgParser<'_>) -> Option<AttributeKind> {
142+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
143+
let _: Result<(), ErrorGuaranteed> = cx.expect_no_args(args);
143144
Some(AttributeKind::ConstStabilityIndirect)
144145
}
145146
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,24 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
322322
},
323323
})
324324
}
325+
326+
pub(crate) fn expect_no_args(&self, args: &ArgParser<'_>) -> Result<(), ErrorGuaranteed> {
327+
if let Some(span) = match args {
328+
ArgParser::NoArgs => None,
329+
ArgParser::List(args) => Some(args.span),
330+
ArgParser::NameValue(args) => Some(args.eq_span.to(args.value_span)),
331+
} {
332+
Err(self.emit_err(AttributeParseError {
333+
span,
334+
attr_span: self.attr_span,
335+
template: self.template.clone(),
336+
attribute: self.attr_path.clone(),
337+
reason: AttributeParseErrorReason::UnexpectedArguments,
338+
}))
339+
} else {
340+
Ok(())
341+
}
342+
}
325343
}
326344

327345
impl<'f, 'sess, S: Stage> Deref for AcceptContext<'f, 'sess, S> {

compiler/rustc_attr_parsing/src/session_diagnostics.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ pub(crate) enum AttributeParseErrorReason {
479479
ExpectedSingleArgument,
480480
ExpectedList,
481481
UnexpectedLiteral,
482+
UnexpectedArguments,
482483
ExpectedNameValue(Option<Symbol>),
483484
DuplicateKey(Symbol),
484485
ExpectedSpecificArgument { possibilities: Vec<&'static str>, strings: bool },
@@ -533,6 +534,9 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for AttributeParseError {
533534
diag.span_label(self.span, format!("didn't expect a literal here"));
534535
diag.code(E0565);
535536
}
537+
AttributeParseErrorReason::UnexpectedArguments => {
538+
diag.span_label(self.span, format!("didn't expect any arguments here"));
539+
}
536540
AttributeParseErrorReason::ExpectedNameValue(None) => {
537541
diag.span_label(
538542
self.span,

compiler/rustc_parse/src/validate_attr.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,9 @@ fn emit_malformed_attribute(
286286
if matches!(
287287
name,
288288
sym::inline
289+
| sym::rustc_as_ptr
290+
| sym::rustc_pub_transparent
291+
| sym::rustc_const_stable_indirect
289292
| sym::rustc_force_inline
290293
| sym::rustc_confusables
291294
| sym::repr

tests/ui/attributes/rustc_skip_during_method_dispatch.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ LL | #[rustc_skip_during_method_dispatch(slice)]
4343
| | valid arguments are `array` or `boxed_slice`
4444
| help: must be of the form: `#[rustc_skip_during_method_dispatch(array, boxed_slice)]`
4545

46-
error[E0565]: malformed `rustc_skip_during_method_dispatch` attribute input
46+
error[E0539]: malformed `rustc_skip_during_method_dispatch` attribute input
4747
--> $DIR/rustc_skip_during_method_dispatch.rs:23:1
4848
|
4949
LL | #[rustc_skip_during_method_dispatch(array = true)]
5050
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^------^^
5151
| | |
52-
| | didn't expect a literal here
52+
| | didn't expect any arguments here
5353
| help: must be of the form: `#[rustc_skip_during_method_dispatch(array, boxed_slice)]`
5454

5555
error[E0565]: malformed `rustc_skip_during_method_dispatch` attribute input

0 commit comments

Comments
 (0)