Skip to content

Commit

Permalink
Rollup merge of rust-lang#128174 - compiler-errors:trait-alias-marker…
Browse files Browse the repository at this point in the history
…, r=oli-obk

Don't record trait aliases as marker traits

Don't record `#[marker]` on trait aliases, since we use that to check for the (non-presence of) associated types and other things which don't make sense of trait aliases. We already enforce this attr is only applied to a trait.

Also do the same for `#[const_trait]`, which we also enforce is only applied to a trait. This is a drive-by change, but also worthwhile just in case.

Fixes rust-lang#127222
  • Loading branch information
matthiaskrgr committed Jul 29, 2024
2 parents 2e9d962 + 12f1463 commit 5551f54
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
14 changes: 9 additions & 5 deletions compiler/rustc_hir_analysis/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1207,25 +1207,29 @@ fn adt_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::AdtDef<'_> {
fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef {
let item = tcx.hir().expect_item(def_id);

let (is_auto, safety, items) = match item.kind {
let (is_alias, is_auto, safety, items) = match item.kind {
hir::ItemKind::Trait(is_auto, safety, .., items) => {
(is_auto == hir::IsAuto::Yes, safety, items)
(false, is_auto == hir::IsAuto::Yes, safety, items)
}
hir::ItemKind::TraitAlias(..) => (false, hir::Safety::Safe, &[][..]),
hir::ItemKind::TraitAlias(..) => (true, false, hir::Safety::Safe, &[][..]),
_ => span_bug!(item.span, "trait_def_of_item invoked on non-trait"),
};

let constness = if tcx.has_attr(def_id, sym::const_trait) {
// Only regular traits can be const.
let constness = if !is_alias && tcx.has_attr(def_id, sym::const_trait) {
hir::Constness::Const
} else {
hir::Constness::NotConst
};

let paren_sugar = tcx.has_attr(def_id, sym::rustc_paren_sugar);
if paren_sugar && !tcx.features().unboxed_closures {
tcx.dcx().emit_err(errors::ParenSugarAttribute { span: item.span });
}

let is_marker = tcx.has_attr(def_id, sym::marker);
// Only regular traits can be marker.
let is_marker = !is_alias && tcx.has_attr(def_id, sym::marker);

let rustc_coinductive = tcx.has_attr(def_id, sym::rustc_coinductive);
let is_fundamental = tcx.has_attr(def_id, sym::fundamental);

Expand Down
3 changes: 0 additions & 3 deletions tests/crashes/127222.rs

This file was deleted.

7 changes: 7 additions & 0 deletions tests/ui/traits/alias/not-a-marker.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#![feature(trait_alias, marker_trait_attr)]

#[marker]
//~^ ERROR attribute should be applied to a trait
trait Foo = Send;

fn main() {}
11 changes: 11 additions & 0 deletions tests/ui/traits/alias/not-a-marker.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error: attribute should be applied to a trait
--> $DIR/not-a-marker.rs:3:1
|
LL | #[marker]
| ^^^^^^^^^
LL |
LL | trait Foo = Send;
| ----------------- not a trait

error: aborting due to 1 previous error

0 comments on commit 5551f54

Please sign in to comment.