Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b830801

Browse files
committedJun 22, 2025·
Port #[rustc_skip_during_method_dispatch] to the new attribute system
1 parent 111e9bc commit b830801

File tree

16 files changed

+233
-33
lines changed

16 files changed

+233
-33
lines changed
 

‎compiler/rustc_attr_data_structures/src/attributes.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,9 @@ pub enum AttributeKind {
253253
/// Represents [`#[repr]`](https://doc.rust-lang.org/stable/reference/type-layout.html#representations).
254254
Repr(ThinVec<(ReprAttr, Span)>),
255255

256+
/// Represents `#[rustc_skip_during_method_dispatch]`.
257+
SkipDuringMethodDispatch { array: bool, boxed_slice: bool, span: Span },
258+
256259
/// Represents `#[stable]`, `#[unstable]` and `#[rustc_allowed_through_unstable_modules]`.
257260
Stability {
258261
stability: Stability,

‎compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,9 @@ impl<S: Stage> SingleAttributeParser<S> for ColdParser {
4848
const TEMPLATE: AttributeTemplate = template!(Word);
4949

5050
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
51-
if !args.no_args() {
52-
cx.expected_no_args(args.span().unwrap_or(cx.attr_span));
53-
return None;
54-
};
55-
51+
if let Err(span) = args.no_args() {
52+
cx.expected_no_args(span);
53+
}
5654
Some(AttributeKind::Cold(cx.attr_span))
5755
}
5856
}

‎compiler/rustc_attr_parsing/src/attributes/lint_helpers.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ 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+
if let Err(span) = args.no_args() {
19+
cx.expected_no_args(span);
20+
}
1921
Some(AttributeKind::AsPtr(cx.attr_span))
2022
}
2123
}

‎compiler/rustc_attr_parsing/src/attributes/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub(crate) mod inline;
3535
pub(crate) mod lint_helpers;
3636
pub(crate) mod must_use;
3737
pub(crate) mod repr;
38+
pub(crate) mod resolution;
3839
pub(crate) mod semantics;
3940
pub(crate) mod stability;
4041
pub(crate) mod transparency;
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use core::mem;
2+
3+
use rustc_attr_data_structures::AttributeKind;
4+
use rustc_feature::{AttributeTemplate, template};
5+
use rustc_span::{Symbol, sym};
6+
7+
use crate::attributes::{AttributeOrder, OnDuplicate, SingleAttributeParser};
8+
use crate::context::{AcceptContext, Stage};
9+
use crate::parser::ArgParser;
10+
11+
pub(crate) struct SkipDuringMethodDispatchParser;
12+
13+
impl<S: Stage> SingleAttributeParser<S> for SkipDuringMethodDispatchParser {
14+
const PATH: &[Symbol] = &[sym::rustc_skip_during_method_dispatch];
15+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepFirst;
16+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
17+
18+
const TEMPLATE: AttributeTemplate = template!(List: "array, boxed_slice");
19+
20+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
21+
let mut array = false;
22+
let mut boxed_slice = false;
23+
let Some(args) = args.list() else {
24+
cx.expected_list(cx.attr_span);
25+
return None;
26+
};
27+
if args.is_empty() {
28+
cx.expected_at_least_one_argument(args.span);
29+
return None;
30+
}
31+
for arg in args.mixed() {
32+
let Some(arg) = arg.meta_item() else {
33+
cx.unexpected_literal(arg.span());
34+
continue;
35+
};
36+
if let Err(span) = arg.args().no_args() {
37+
cx.expected_no_args(span);
38+
}
39+
let path = arg.path();
40+
let (key, skip): (Symbol, &mut bool) = match path.word_sym() {
41+
Some(key @ sym::array) => (key, &mut array),
42+
Some(key @ sym::boxed_slice) => (key, &mut boxed_slice),
43+
_ => {
44+
cx.expected_specific_argument(path.span(), vec!["array", "boxed_slice"]);
45+
continue;
46+
}
47+
};
48+
if mem::replace(skip, true) {
49+
cx.duplicate_key(arg.span(), key);
50+
}
51+
}
52+
Some(AttributeKind::SkipDuringMethodDispatch { array, boxed_slice, span: cx.attr_span })
53+
}
54+
}

‎compiler/rustc_attr_parsing/src/attributes/stability.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,10 @@ 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+
if let Err(span) = args.no_args() {
144+
cx.expected_no_args(span);
145+
}
143146
Some(AttributeKind::ConstStabilityIndirect)
144147
}
145148
}
@@ -361,7 +364,7 @@ pub(crate) fn parse_unstability<S: Stage>(
361364
};
362365
}
363366
Some(sym::soft) => {
364-
if !param.args().no_args() {
367+
if !param.args().is_no_args() {
365368
cx.emit_err(session_diagnostics::SoftNoArgs { span: param.span() });
366369
}
367370
is_soft = true;

‎compiler/rustc_attr_parsing/src/context.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use crate::attributes::inline::{InlineParser, RustcForceInlineParser};
2222
use crate::attributes::lint_helpers::{AsPtrParser, PubTransparentParser};
2323
use crate::attributes::must_use::MustUseParser;
2424
use crate::attributes::repr::{AlignParser, ReprParser};
25+
use crate::attributes::resolution::SkipDuringMethodDispatchParser;
2526
use crate::attributes::semantics::MayDangleParser;
2627
use crate::attributes::stability::{
2728
BodyStabilityParser, ConstStabilityIndirectParser, ConstStabilityParser, StabilityParser,
@@ -117,6 +118,7 @@ attribute_parsers!(
117118
Single<OptimizeParser>,
118119
Single<PubTransparentParser>,
119120
Single<RustcForceInlineParser>,
121+
Single<SkipDuringMethodDispatchParser>,
120122
Single<TransparencyParser>,
121123
// tidy-alphabetical-end
122124
];
@@ -295,6 +297,16 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
295297
})
296298
}
297299

300+
pub(crate) fn expected_at_least_one_argument(&self, span: Span) -> ErrorGuaranteed {
301+
self.emit_err(AttributeParseError {
302+
span,
303+
attr_span: self.attr_span,
304+
template: self.template.clone(),
305+
attribute: self.attr_path.clone(),
306+
reason: AttributeParseErrorReason::ExpectedAtLeastOneArgument,
307+
})
308+
}
309+
298310
pub(crate) fn expected_specific_argument(
299311
&self,
300312
span: Span,

‎compiler/rustc_attr_parsing/src/parser.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,20 @@ impl<'a> ArgParser<'a> {
162162
}
163163

164164
/// Asserts that there are no arguments
165-
pub fn no_args(&self) -> bool {
165+
pub fn is_no_args(&self) -> bool {
166166
matches!(self, Self::NoArgs)
167167
}
168+
169+
/// Assert that there were no args.
170+
/// If there were, get a span to the arguments
171+
/// (to pass to [`AcceptContext::expected_no_args`](crate::context::AcceptContext::expected_no_args)).
172+
pub fn no_args(&self) -> Result<(), Span> {
173+
match self {
174+
Self::NoArgs => Ok(()),
175+
Self::List(args) => Err(args.span),
176+
Self::NameValue(args) => Err(args.eq_span.to(args.value_span)),
177+
}
178+
}
168179
}
169180

170181
/// Inside lists, values could be either literals, or more deeply nested meta items.

‎compiler/rustc_attr_parsing/src/session_diagnostics.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,7 @@ pub(crate) struct UnrecognizedReprHint {
485485
pub(crate) enum AttributeParseErrorReason {
486486
ExpectedNoArgs,
487487
ExpectedStringLiteral { byte_string: Option<Span> },
488+
ExpectedAtLeastOneArgument,
488489
ExpectedSingleArgument,
489490
ExpectedList,
490491
UnexpectedLiteral,
@@ -528,6 +529,9 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for AttributeParseError {
528529
diag.span_label(self.span, "expected a single argument here");
529530
diag.code(E0805);
530531
}
532+
AttributeParseErrorReason::ExpectedAtLeastOneArgument => {
533+
diag.span_label(self.span, "expected at least 1 argument here");
534+
}
531535
AttributeParseErrorReason::ExpectedList => {
532536
diag.span_label(self.span, "expected this to be a list");
533537
}

‎compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,7 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
10831083
"the `#[rustc_main]` attribute is used internally to specify test entry point function",
10841084
),
10851085
rustc_attr!(
1086-
rustc_skip_during_method_dispatch, Normal, template!(List: "array, boxed_slice"), WarnFollowing,
1086+
rustc_skip_during_method_dispatch, Normal, template!(List: "array, boxed_slice"), ErrorFollowing,
10871087
EncodeCrossCrate::No,
10881088
"the `#[rustc_skip_during_method_dispatch]` attribute is used to exclude a trait \
10891089
from method dispatch when the receiver is of the following type, for compatibility in \

‎compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use std::ops::Bound;
2121

2222
use rustc_abi::ExternAbi;
2323
use rustc_ast::Recovered;
24+
use rustc_attr_data_structures::{AttributeKind, find_attr};
2425
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
2526
use rustc_data_structures::unord::UnordMap;
2627
use rustc_errors::{
@@ -1151,22 +1152,11 @@ fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef {
11511152
let rustc_coinductive = tcx.has_attr(def_id, sym::rustc_coinductive);
11521153
let is_fundamental = tcx.has_attr(def_id, sym::fundamental);
11531154

1154-
// FIXME: We could probably do way better attribute validation here.
1155-
let mut skip_array_during_method_dispatch = false;
1156-
let mut skip_boxed_slice_during_method_dispatch = false;
1157-
for attr in tcx.get_attrs(def_id, sym::rustc_skip_during_method_dispatch) {
1158-
if let Some(lst) = attr.meta_item_list() {
1159-
for item in lst {
1160-
if let Some(ident) = item.ident() {
1161-
match ident.as_str() {
1162-
"array" => skip_array_during_method_dispatch = true,
1163-
"boxed_slice" => skip_boxed_slice_during_method_dispatch = true,
1164-
_ => (),
1165-
}
1166-
}
1167-
}
1168-
}
1169-
}
1155+
let [skip_array_during_method_dispatch, skip_boxed_slice_during_method_dispatch] = find_attr!(
1156+
tcx.get_all_attrs(def_id),
1157+
AttributeKind::SkipDuringMethodDispatch { array, boxed_slice, span:_ } => [*array, *boxed_slice]
1158+
)
1159+
.unwrap_or([false; 2]);
11701160

11711161
let specialization_kind = if tcx.has_attr(def_id, sym::rustc_unsafe_specialization_marker) {
11721162
ty::trait_def::TraitSpecializationKind::Marker

‎compiler/rustc_parse/src/validate_attr.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,12 @@ 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
294+
| sym::rustc_skip_during_method_dispatch
291295
| sym::repr
292296
| sym::align
293297
| sym::deprecated

‎compiler/rustc_passes/src/check_attr.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
118118
for attr in attrs {
119119
let mut style = None;
120120
match attr {
121+
Attribute::Parsed(AttributeKind::SkipDuringMethodDispatch {
122+
span: attr_span,
123+
..
124+
}) => {
125+
self.check_must_be_applied_to_trait(*attr_span, span, target);
126+
}
121127
Attribute::Parsed(AttributeKind::Confusables { first_span, .. }) => {
122128
self.check_confusables(*first_span, target);
123129
}
@@ -245,7 +251,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
245251
| [sym::rustc_must_implement_one_of, ..]
246252
| [sym::rustc_deny_explicit_impl, ..]
247253
| [sym::rustc_do_not_implement_via_object, ..]
248-
| [sym::const_trait, ..] => self.check_must_be_applied_to_trait(attr, span, target),
254+
| [sym::const_trait, ..] => self.check_must_be_applied_to_trait(attr.span(), span, target),
249255
[sym::collapse_debuginfo, ..] => self.check_collapse_debuginfo(attr, span, target),
250256
[sym::must_not_suspend, ..] => self.check_must_not_suspend(attr, span, target),
251257
[sym::rustc_pass_by_value, ..] => self.check_pass_by_value(attr, span, target),
@@ -1915,14 +1921,11 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
19151921
}
19161922

19171923
/// Checks if the attribute is applied to a trait.
1918-
fn check_must_be_applied_to_trait(&self, attr: &Attribute, span: Span, target: Target) {
1924+
fn check_must_be_applied_to_trait(&self, attr_span: Span, defn_span: Span, target: Target) {
19191925
match target {
19201926
Target::Trait => {}
19211927
_ => {
1922-
self.dcx().emit_err(errors::AttrShouldBeAppliedToTrait {
1923-
attr_span: attr.span(),
1924-
defn_span: span,
1925-
});
1928+
self.dcx().emit_err(errors::AttrShouldBeAppliedToTrait { attr_span, defn_span });
19261929
}
19271930
}
19281931
}

‎compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,7 @@ symbols! {
577577
box_new,
578578
box_patterns,
579579
box_syntax,
580+
boxed_slice,
580581
bpf_target_feature,
581582
braced_empty_structs,
582583
branch,
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#![feature(rustc_attrs)]
2+
3+
#[rustc_skip_during_method_dispatch]
4+
//~^ ERROR: malformed `rustc_skip_during_method_dispatch` attribute input [E0539]
5+
trait NotAList {}
6+
7+
#[rustc_skip_during_method_dispatch = "array"]
8+
//~^ ERROR: malformed `rustc_skip_during_method_dispatch` attribute input [E0539]
9+
trait AlsoNotAList {}
10+
11+
#[rustc_skip_during_method_dispatch()]
12+
//~^ ERROR: malformed `rustc_skip_during_method_dispatch` attribute input
13+
trait Argless {}
14+
15+
#[rustc_skip_during_method_dispatch(array, boxed_slice, array)]
16+
//~^ ERROR: malformed `rustc_skip_during_method_dispatch` attribute input
17+
trait Duplicate {}
18+
19+
#[rustc_skip_during_method_dispatch(slice)]
20+
//~^ ERROR: malformed `rustc_skip_during_method_dispatch` attribute input
21+
trait Unexpected {}
22+
23+
#[rustc_skip_during_method_dispatch(array = true)]
24+
//~^ ERROR: malformed `rustc_skip_during_method_dispatch` attribute input
25+
trait KeyValue {}
26+
27+
#[rustc_skip_during_method_dispatch("array")]
28+
//~^ ERROR: malformed `rustc_skip_during_method_dispatch` attribute input
29+
trait String {}
30+
31+
#[rustc_skip_during_method_dispatch(array, boxed_slice)]
32+
trait OK {}
33+
34+
#[rustc_skip_during_method_dispatch(array)]
35+
//~^ ERROR: attribute should be applied to a trait
36+
impl OK for () {}
37+
38+
fn main() {}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
error[E0539]: malformed `rustc_skip_during_method_dispatch` attribute input
2+
--> $DIR/rustc_skip_during_method_dispatch.rs:3:1
3+
|
4+
LL | #[rustc_skip_during_method_dispatch]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
| |
7+
| expected this to be a list
8+
| help: must be of the form: `#[rustc_skip_during_method_dispatch(array, boxed_slice)]`
9+
10+
error[E0539]: malformed `rustc_skip_during_method_dispatch` attribute input
11+
--> $DIR/rustc_skip_during_method_dispatch.rs:7:1
12+
|
13+
LL | #[rustc_skip_during_method_dispatch = "array"]
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15+
| |
16+
| expected this to be a list
17+
| help: must be of the form: `#[rustc_skip_during_method_dispatch(array, boxed_slice)]`
18+
19+
error[E0539]: malformed `rustc_skip_during_method_dispatch` attribute input
20+
--> $DIR/rustc_skip_during_method_dispatch.rs:11:1
21+
|
22+
LL | #[rustc_skip_during_method_dispatch()]
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^--^
24+
| | |
25+
| | expected at least 1 argument here
26+
| help: must be of the form: `#[rustc_skip_during_method_dispatch(array, boxed_slice)]`
27+
28+
error[E0538]: malformed `rustc_skip_during_method_dispatch` attribute input
29+
--> $DIR/rustc_skip_during_method_dispatch.rs:15:1
30+
|
31+
LL | #[rustc_skip_during_method_dispatch(array, boxed_slice, array)]
32+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-----^^
33+
| | |
34+
| | found `array` used as a key more than once
35+
| help: must be of the form: `#[rustc_skip_during_method_dispatch(array, boxed_slice)]`
36+
37+
error[E0539]: malformed `rustc_skip_during_method_dispatch` attribute input
38+
--> $DIR/rustc_skip_during_method_dispatch.rs:19:1
39+
|
40+
LL | #[rustc_skip_during_method_dispatch(slice)]
41+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-----^^
42+
| | |
43+
| | valid arguments are `array` or `boxed_slice`
44+
| help: must be of the form: `#[rustc_skip_during_method_dispatch(array, boxed_slice)]`
45+
46+
error[E0565]: malformed `rustc_skip_during_method_dispatch` attribute input
47+
--> $DIR/rustc_skip_during_method_dispatch.rs:23:1
48+
|
49+
LL | #[rustc_skip_during_method_dispatch(array = true)]
50+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^------^^
51+
| | |
52+
| | didn't expect any arguments here
53+
| help: must be of the form: `#[rustc_skip_during_method_dispatch(array, boxed_slice)]`
54+
55+
error[E0565]: malformed `rustc_skip_during_method_dispatch` attribute input
56+
--> $DIR/rustc_skip_during_method_dispatch.rs:27:1
57+
|
58+
LL | #[rustc_skip_during_method_dispatch("array")]
59+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-------^^
60+
| | |
61+
| | didn't expect a literal here
62+
| help: must be of the form: `#[rustc_skip_during_method_dispatch(array, boxed_slice)]`
63+
64+
error: attribute should be applied to a trait
65+
--> $DIR/rustc_skip_during_method_dispatch.rs:34:1
66+
|
67+
LL | #[rustc_skip_during_method_dispatch(array)]
68+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
69+
LL |
70+
LL | impl OK for () {}
71+
| ----------------- not a trait
72+
73+
error: aborting due to 8 previous errors
74+
75+
Some errors have detailed explanations: E0538, E0539, E0565.
76+
For more information about an error, try `rustc --explain E0538`.

0 commit comments

Comments
 (0)
Please sign in to comment.