about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMichael Goulet <michael@errs.io>2024-07-25 00:32:50 -0400
committerMichael Goulet <michael@errs.io>2024-07-25 00:38:50 -0400
commit12f1463b7e9b1d01b5e46b50d375de0a9026f2b1 (patch)
tree13b5136dd4852b13d0c17d8da6af52e4cf05424b
parent2ccafed862f6906707a390caf180449dd64cad2e (diff)
downloadrust-12f1463b7e9b1d01b5e46b50d375de0a9026f2b1.tar.gz
rust-12f1463b7e9b1d01b5e46b50d375de0a9026f2b1.zip
Don't record trait aliases as marker traits
-rw-r--r--compiler/rustc_hir_analysis/src/collect.rs14
-rw-r--r--tests/crashes/127222.rs3
-rw-r--r--tests/ui/traits/alias/not-a-marker.rs7
-rw-r--r--tests/ui/traits/alias/not-a-marker.stderr11
4 files changed, 27 insertions, 8 deletions
diff --git a/compiler/rustc_hir_analysis/src/collect.rs b/compiler/rustc_hir_analysis/src/collect.rs
index 565351268c9..71f466ef5da 100644
--- a/compiler/rustc_hir_analysis/src/collect.rs
+++ b/compiler/rustc_hir_analysis/src/collect.rs
@@ -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);
 
diff --git a/tests/crashes/127222.rs b/tests/crashes/127222.rs
deleted file mode 100644
index eda0ea3d9b7..00000000000
--- a/tests/crashes/127222.rs
+++ /dev/null
@@ -1,3 +0,0 @@
-//@ known-bug: rust-lang/rust#127222
-#[marker]
-trait Foo = PartialEq<i32> + Send;
diff --git a/tests/ui/traits/alias/not-a-marker.rs b/tests/ui/traits/alias/not-a-marker.rs
new file mode 100644
index 00000000000..b004b9ff9ae
--- /dev/null
+++ b/tests/ui/traits/alias/not-a-marker.rs
@@ -0,0 +1,7 @@
+#![feature(trait_alias, marker_trait_attr)]
+
+#[marker]
+//~^ ERROR attribute should be applied to a trait
+trait Foo = Send;
+
+fn main() {}
diff --git a/tests/ui/traits/alias/not-a-marker.stderr b/tests/ui/traits/alias/not-a-marker.stderr
new file mode 100644
index 00000000000..2f3f6fea30f
--- /dev/null
+++ b/tests/ui/traits/alias/not-a-marker.stderr
@@ -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
+