Skip to content

Commit 73baa93

Browse files
committed
Introduce #[rustc_auto_trait]
1 parent a1c7a1c commit 73baa93

File tree

15 files changed

+108
-38
lines changed

15 files changed

+108
-38
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
443443
items: new_impl_items,
444444
}))
445445
}
446-
ItemKind::Trait(box Trait { is_auto, unsafety, generics, bounds, items }) => {
446+
// We intentionally ignore `is_auto` since `auto` is now meaningless.
447+
ItemKind::Trait(box Trait { is_auto: _, unsafety, generics, bounds, items }) => {
448+
let is_auto =
449+
if attrs.unwrap_or(&[]).iter().any(|attr| attr.has_name(sym::rustc_auto_trait))
450+
{
451+
IsAuto::Yes
452+
} else {
453+
IsAuto::No
454+
};
447455
// FIXME(const_trait_impl, effects, fee1-dead) this should be simplified if possible
448456
let constness = attrs
449457
.unwrap_or(&[])
@@ -467,7 +475,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
467475
(unsafety, items, bounds)
468476
},
469477
);
470-
hir::ItemKind::Trait(*is_auto, unsafety, generics, bounds, items)
478+
hir::ItemKind::Trait(is_auto, unsafety, generics, bounds, items)
471479
}
472480
ItemKind::TraitAlias(generics, bounds) => {
473481
let (generics, bounds) = self.lower_generics(

compiler/rustc_ast_passes/messages.ftl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@ ast_passes_nested_lifetimes = nested quantification of lifetimes
184184
185185
ast_passes_nomangle_ascii = `#[no_mangle]` requires ASCII identifier
186186
187-
ast_passes_obsolete_auto = `impl Trait for .. {"{}"}` is an obsolete syntax
188-
.help = use `auto trait Trait {"{}"}` instead
187+
ast_passes_obsolete_auto_syntax = `{$syntax}` is an obsolete syntax for auto traits
188+
.help = use `#[rustc_auto_trait] trait Trait {"{}"}` instead
189189
190190
ast_passes_optional_const_exclusive = `~const` and `{$modifier}` are mutually exclusive
191191

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,10 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
801801
errors::VisibilityNotPermittedNote::TraitImpl,
802802
);
803803
if let TyKind::Err = self_ty.kind {
804-
this.err_handler().emit_err(errors::ObsoleteAuto { span: item.span });
804+
this.err_handler().emit_err(errors::ObsoleteAutoSyntax {
805+
span: item.span,
806+
syntax: "impl Trait for .. {}",
807+
});
805808
}
806809
if let (&Unsafe::Yes(span), &ImplPolarity::Negative(sp)) = (unsafety, polarity)
807810
{
@@ -932,8 +935,9 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
932935
}
933936
}
934937
}
935-
ItemKind::Trait(box Trait { is_auto, generics, bounds, items, .. }) => {
936-
if *is_auto == IsAuto::Yes {
938+
// We intentionally ignore `is_auto` since `auto` is now meaningless.
939+
ItemKind::Trait(box Trait { is_auto: _, generics, bounds, items, .. }) => {
940+
if attr::contains_name(&item.attrs, sym::rustc_auto_trait) {
937941
// Auto traits cannot have generics, super traits nor contain items.
938942
self.deny_generic_params(generics, item.ident.span);
939943
self.deny_super_traits(bounds, item.ident.span);

compiler/rustc_ast_passes/src/errors.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,11 +417,12 @@ pub struct OutOfOrderParams<'a> {
417417
}
418418

419419
#[derive(Diagnostic)]
420-
#[diag(ast_passes_obsolete_auto)]
420+
#[diag(ast_passes_obsolete_auto_syntax)]
421421
#[help]
422-
pub struct ObsoleteAuto {
422+
pub struct ObsoleteAutoSyntax {
423423
#[primary_span]
424424
pub span: Span,
425+
pub syntax: &'static str,
425426
}
426427

427428
#[derive(Diagnostic)]

compiler/rustc_ast_passes/src/feature_gate.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -298,12 +298,11 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
298298
}
299299

300300
ast::ItemKind::Trait(box ast::Trait { is_auto: ast::IsAuto::Yes, .. }) => {
301-
gate_feature_post!(
302-
&self,
303-
auto_traits,
304-
i.span,
305-
"auto traits are experimental and possibly buggy"
306-
);
301+
// FIXME: Consider using a structured suggestion.
302+
self.sess.emit_err(errors::ObsoleteAutoSyntax {
303+
span: i.span,
304+
syntax: "auto trait Trait {}",
305+
});
307306
}
308307

309308
ast::ItemKind::TraitAlias(..) => {

compiler/rustc_feature/src/active.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,6 @@ declare_features! (
223223
// feature-group-start: internal feature gates
224224
// -------------------------------------------------------------------------
225225

226-
/// Allows features specific to auto traits.
227-
/// Renamed from `optin_builtin_traits`.
228-
(active, auto_traits, "1.50.0", Some(13231), None),
229226
/// Allows using `box` in patterns (RFC 469).
230227
(active, box_patterns, "1.0.0", Some(29641), None),
231228
/// Allows `#[doc(notable_trait)]`.

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,10 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
551551
may_dangle, Normal, template!(Word), WarnFollowing, dropck_eyepatch,
552552
"`may_dangle` has unstable semantics and may be removed in the future",
553553
),
554+
rustc_attr!(
555+
rustc_auto_trait, Normal, template!(Word), WarnFollowing,
556+
"`#[rustc_auto_trait]` is used to mark auto traits, only intended to be used in `core`",
557+
),
554558

555559
// ==========================================================================
556560
// Internal attributes: Runtime related:
@@ -709,7 +713,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
709713
),
710714
rustc_attr!(
711715
rustc_coinductive, AttributeType::Normal, template!(Word), WarnFollowing, @only_local: true,
712-
"#![rustc_coinductive] changes a trait to be coinductive, allowing cycles in the trait solver."
716+
"#[rustc_coinductive] changes a trait to be coinductive, allowing cycles in the trait solver."
713717
),
714718
rustc_attr!(
715719
rustc_allow_incoherent_impl, AttributeType::Normal, template!(Word), ErrorFollowing, @only_local: true,

compiler/rustc_feature/src/removed.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ declare_features! (
5050
(removed, allocator, "1.0.0", None, None, None),
5151
/// Allows a test to fail without failing the whole suite.
5252
(removed, allow_fail, "1.19.0", Some(46488), None, Some("removed due to no clear use cases")),
53+
/// Allows features specific to auto traits.
54+
/// Renamed from `optin_builtin_traits`.
55+
(removed, auto_traits, "1.50.0", Some(13231), None, Some("removed in favor of `#[feature(rustc_attrs)]`")),
5356
(removed, await_macro, "1.38.0", Some(50547), None,
5457
Some("subsumed by `.await` syntax")),
5558
/// Allows using the `box $expr` syntax.
@@ -147,9 +150,8 @@ declare_features! (
147150
/// A way to temporarily opt out of opt-in copy. This will *never* be accepted.
148151
(removed, opt_out_copy, "1.0.0", None, None, None),
149152
/// Allows features specific to OIBIT (now called auto traits).
150-
/// Renamed to `auto_traits`.
151-
(removed, optin_builtin_traits, "1.0.0", Some(13231), None,
152-
Some("renamed to `auto_traits`")),
153+
/// Renamed to `auto_traits` which has then been removed in favor of `rustc_attrs`.
154+
(removed, optin_builtin_traits, "1.0.0", Some(13231), None, Some("removed in favor of `#[feature(rustc_attrs)]`")),
153155
/// Allows overlapping impls of marker traits.
154156
(removed, overlapping_marker_traits, "1.42.0", Some(29864), None,
155157
Some("removed in favor of `#![feature(marker_trait_attr)]`")),

compiler/rustc_passes/src/check_attr.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,13 @@ impl CheckAttrVisitor<'_> {
167167
| sym::rustc_dirty
168168
| sym::rustc_if_this_changed
169169
| sym::rustc_then_this_would_need => self.check_rustc_dirty_clean(&attr),
170-
sym::rustc_coinductive
171-
| sym::rustc_must_implement_one_of
170+
sym::const_trait
171+
| sym::rustc_auto_trait
172+
| sym::rustc_coinductive
172173
| sym::rustc_deny_explicit_impl
173-
| sym::const_trait => self.check_must_be_applied_to_trait(&attr, span, target),
174+
| sym::rustc_must_implement_one_of => {
175+
self.check_must_be_applied_to_trait(&attr, span, target)
176+
}
174177
sym::cmse_nonsecure_entry => {
175178
self.check_cmse_nonsecure_entry(hir_id, attr, span, target)
176179
}

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,6 +1292,7 @@ symbols! {
12921292
rustc_allow_incoherent_impl,
12931293
rustc_allowed_through_unstable_modules,
12941294
rustc_attrs,
1295+
rustc_auto_trait,
12951296
rustc_box,
12961297
rustc_builtin_macro,
12971298
rustc_capture_analysis,

0 commit comments

Comments
 (0)