Skip to content

Commit 5551f54

Browse files
authored
Rollup merge of rust-lang#128174 - compiler-errors:trait-alias-marker, 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
2 parents 2e9d962 + 12f1463 commit 5551f54

File tree

4 files changed

+27
-8
lines changed

4 files changed

+27
-8
lines changed

Diff for: compiler/rustc_hir_analysis/src/collect.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -1207,25 +1207,29 @@ fn adt_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::AdtDef<'_> {
12071207
fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef {
12081208
let item = tcx.hir().expect_item(def_id);
12091209

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

1218-
let constness = if tcx.has_attr(def_id, sym::const_trait) {
1218+
// Only regular traits can be const.
1219+
let constness = if !is_alias && tcx.has_attr(def_id, sym::const_trait) {
12191220
hir::Constness::Const
12201221
} else {
12211222
hir::Constness::NotConst
12221223
};
1224+
12231225
let paren_sugar = tcx.has_attr(def_id, sym::rustc_paren_sugar);
12241226
if paren_sugar && !tcx.features().unboxed_closures {
12251227
tcx.dcx().emit_err(errors::ParenSugarAttribute { span: item.span });
12261228
}
12271229

1228-
let is_marker = tcx.has_attr(def_id, sym::marker);
1230+
// Only regular traits can be marker.
1231+
let is_marker = !is_alias && tcx.has_attr(def_id, sym::marker);
1232+
12291233
let rustc_coinductive = tcx.has_attr(def_id, sym::rustc_coinductive);
12301234
let is_fundamental = tcx.has_attr(def_id, sym::fundamental);
12311235

Diff for: tests/crashes/127222.rs

-3
This file was deleted.

Diff for: tests/ui/traits/alias/not-a-marker.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#![feature(trait_alias, marker_trait_attr)]
2+
3+
#[marker]
4+
//~^ ERROR attribute should be applied to a trait
5+
trait Foo = Send;
6+
7+
fn main() {}

Diff for: tests/ui/traits/alias/not-a-marker.stderr

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: attribute should be applied to a trait
2+
--> $DIR/not-a-marker.rs:3:1
3+
|
4+
LL | #[marker]
5+
| ^^^^^^^^^
6+
LL |
7+
LL | trait Foo = Send;
8+
| ----------------- not a trait
9+
10+
error: aborting due to 1 previous error
11+

0 commit comments

Comments
 (0)