Skip to content

Commit cfda2a3

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

File tree

15 files changed

+103
-32
lines changed

15 files changed

+103
-32
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -443,10 +443,16 @@ 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 trait` is now meaningless.
447+
ItemKind::Trait(box Trait { is_auto: _, unsafety, generics, bounds, items }) => {
448+
let attrs = attrs.unwrap_or(&[]);
449+
let is_auto = if attrs.iter().any(|attr| attr.has_name(sym::rustc_auto_trait)) {
450+
IsAuto::Yes
451+
} else {
452+
IsAuto::No
453+
};
447454
// FIXME(const_trait_impl, effects, fee1-dead) this should be simplified if possible
448455
let constness = attrs
449-
.unwrap_or(&[])
450456
.iter()
451457
.find(|x| x.has_name(sym::const_trait))
452458
.map_or(Const::No, |x| Const::Yes(x.span));
@@ -467,7 +473,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
467473
(unsafety, items, bounds)
468474
},
469475
);
470-
hir::ItemKind::Trait(*is_auto, unsafety, generics, bounds, items)
476+
hir::ItemKind::Trait(is_auto, unsafety, generics, bounds, items)
471477
}
472478
ItemKind::TraitAlias(generics, bounds) => {
473479
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 trait` 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: Considering 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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ declare_features! (
225225

226226
/// Allows features specific to auto traits.
227227
/// Renamed from `optin_builtin_traits`.
228-
(active, auto_traits, "1.50.0", Some(13231), None),
228+
(internal, auto_traits, "1.50.0", Some(13231), None),
229229
/// Allows using `box` in patterns (RFC 469).
230230
(active, box_patterns, "1.0.0", Some(29641), None),
231231
/// 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+
gated!(
555+
rustc_auto_trait, Normal, template!(Word), WarnFollowing, auto_traits,
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_hir_analysis/src/collect.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,7 @@ fn adt_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::AdtDef<'_> {
893893
fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef {
894894
let item = tcx.hir().expect_item(def_id);
895895

896+
// FIXME: Maybe check `tcx.has_attr(def_id, sym::rustc_auto_trait)` **here**!
896897
let (is_auto, unsafety, items) = match item.kind {
897898
hir::ItemKind::Trait(is_auto, unsafety, .., items) => {
898899
(is_auto == hir::IsAuto::Yes, unsafety, items)

compiler/rustc_hir_typeck/src/method/suggest.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
805805
entry.1.insert((self_ty.span, ""));
806806
}
807807
Some(Node::Item(hir::Item {
808-
kind: hir::ItemKind::Trait(rustc_ast::ast::IsAuto::Yes, ..),
808+
kind: hir::ItemKind::Trait(rustc_hir::IsAuto::Yes, ..),
809809
span: item_span,
810810
..
811811
})) => {

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 // FIXME: does this correctly reject trait *aliases*?
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
}

0 commit comments

Comments
 (0)