Skip to content

Commit 5be2ec7

Browse files
committed
Auto merge of rust-lang#127200 - fee1-dead-contrib:trait_def_const_trait, r=compiler-errors
Add `constness` to `TraitDef` Second attempt at fixing the regression @ rust-lang#120639 (comment) r? project-const-traits
2 parents cd3d98b + 46af987 commit 5be2ec7

File tree

9 files changed

+41
-30
lines changed

9 files changed

+41
-30
lines changed

compiler/rustc_ast_lowering/src/item.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -625,9 +625,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
625625
_ => Const::No,
626626
}
627627
} else {
628-
self.tcx
629-
.get_attr(def_id, sym::const_trait)
630-
.map_or(Const::No, |attr| Const::Yes(attr.span))
628+
if self.tcx.is_const_trait(def_id) {
629+
// FIXME(effects) span
630+
Const::Yes(self.tcx.def_ident_span(def_id).unwrap())
631+
} else {
632+
Const::No
633+
}
631634
}
632635
} else {
633636
Const::No

compiler/rustc_feature/src/builtin_attrs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
556556
),
557557
// RFC 2632
558558
gated!(
559-
const_trait, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::Yes, const_trait_impl,
559+
const_trait, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::No, const_trait_impl,
560560
"`const_trait` is a temporary placeholder for marking a trait that is suitable for `const` \
561561
`impls` and all default bodies as `const`, which may be removed or renamed in the \
562562
future."

compiler/rustc_hir_analysis/src/bounds.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_hir::LangItem;
77
use rustc_middle::ty::fold::FnMutDelegate;
88
use rustc_middle::ty::{self, Ty, TyCtxt, Upcast};
99
use rustc_span::def_id::DefId;
10-
use rustc_span::{sym, Span};
10+
use rustc_span::Span;
1111

1212
/// Collects together a list of type bounds. These lists of bounds occur in many places
1313
/// in Rust's syntax:
@@ -80,7 +80,7 @@ impl<'tcx> Bounds<'tcx> {
8080
}
8181

8282
(_, ty::BoundConstness::NotConst) => {
83-
if !tcx.has_attr(bound_trait_ref.def_id(), sym::const_trait) {
83+
if !tcx.is_const_trait(bound_trait_ref.def_id()) {
8484
return;
8585
}
8686
tcx.consts.true_

compiler/rustc_hir_analysis/src/collect.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1194,6 +1194,11 @@ fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef {
11941194
_ => span_bug!(item.span, "trait_def_of_item invoked on non-trait"),
11951195
};
11961196

1197+
let constness = if tcx.has_attr(def_id, sym::const_trait) {
1198+
hir::Constness::Const
1199+
} else {
1200+
hir::Constness::NotConst
1201+
};
11971202
let paren_sugar = tcx.has_attr(def_id, sym::rustc_paren_sugar);
11981203
if paren_sugar && !tcx.features().unboxed_closures {
11991204
tcx.dcx().emit_err(errors::ParenSugarAttribute { span: item.span });
@@ -1349,6 +1354,7 @@ fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef {
13491354
ty::TraitDef {
13501355
def_id: def_id.to_def_id(),
13511356
safety,
1357+
constness,
13521358
paren_sugar,
13531359
has_auto_impl: is_auto,
13541360
is_marker,
@@ -1682,7 +1688,7 @@ fn check_impl_constness(
16821688
}
16831689

16841690
let trait_def_id = hir_trait_ref.trait_def_id()?;
1685-
if tcx.has_attr(trait_def_id, sym::const_trait) {
1691+
if tcx.is_const_trait(trait_def_id) {
16861692
return None;
16871693
}
16881694

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ use rustc_middle::{bug, span_bug};
4747
use rustc_session::lint::builtin::AMBIGUOUS_ASSOCIATED_ITEMS;
4848
use rustc_span::edit_distance::find_best_match_for_name;
4949
use rustc_span::symbol::{kw, Ident, Symbol};
50-
use rustc_span::{sym, Span, DUMMY_SP};
50+
use rustc_span::{Span, DUMMY_SP};
5151
use rustc_target::spec::abi;
5252
use rustc_trait_selection::infer::InferCtxtExt;
5353
use rustc_trait_selection::traits::wf::object_region_bounds;
@@ -560,7 +560,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
560560
}
561561
if let ty::BoundConstness::Const | ty::BoundConstness::ConstIfConst = constness
562562
&& generics.has_self
563-
&& !tcx.has_attr(def_id, sym::const_trait)
563+
&& !tcx.is_const_trait(def_id)
564564
{
565565
let reported = tcx.dcx().emit_err(crate::errors::ConstBoundForNonConstTrait {
566566
span,
@@ -1848,19 +1848,13 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
18481848
path.segments[..path.segments.len() - 2].iter(),
18491849
GenericsArgsErrExtend::None,
18501850
);
1851-
// HACK: until we support `<Type as ~const Trait>`, assume all of them are.
1852-
let constness = if tcx.has_attr(tcx.parent(def_id), sym::const_trait) {
1853-
ty::BoundConstness::ConstIfConst
1854-
} else {
1855-
ty::BoundConstness::NotConst
1856-
};
18571851
self.lower_qpath(
18581852
span,
18591853
opt_self_ty,
18601854
def_id,
18611855
&path.segments[path.segments.len() - 2],
18621856
path.segments.last().unwrap(),
1863-
constness,
1857+
ty::BoundConstness::NotConst,
18641858
)
18651859
}
18661860
Res::PrimTy(prim_ty) => {

compiler/rustc_hir_typeck/src/method/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_middle::ty::{self, GenericParamDefKind, Ty, TypeVisitableExt};
2121
use rustc_middle::ty::{GenericArgs, GenericArgsRef};
2222
use rustc_middle::{bug, span_bug};
2323
use rustc_span::symbol::Ident;
24-
use rustc_span::{sym, Span};
24+
use rustc_span::Span;
2525
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
2626
use rustc_trait_selection::traits::{self, NormalizeExt};
2727

@@ -359,7 +359,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
359359
// FIXME(effects) find a better way to do this
360360
// Operators don't have generic methods, but making them `#[const_trait]` gives them
361361
// `const host: bool`.
362-
let args = if self.tcx.has_attr(trait_def_id, sym::const_trait) {
362+
let args = if self.tcx.is_const_trait(trait_def_id) {
363363
self.tcx.mk_args_from_iter(
364364
args.iter()
365365
.chain([self.tcx.expected_host_effect_param_for_body(self.body_id).into()]),

compiler/rustc_middle/src/ty/mod.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1967,9 +1967,14 @@ impl<'tcx> TyCtxt<'tcx> {
19671967
) && self.constness(def_id) == hir::Constness::Const
19681968
}
19691969

1970+
#[inline]
1971+
pub fn is_const_trait(self, def_id: DefId) -> bool {
1972+
self.trait_def(def_id).constness == hir::Constness::Const
1973+
}
1974+
19701975
#[inline]
19711976
pub fn is_const_default_method(self, def_id: DefId) -> bool {
1972-
matches!(self.trait_of_item(def_id), Some(trait_id) if self.has_attr(trait_id, sym::const_trait))
1977+
matches!(self.trait_of_item(def_id), Some(trait_id) if self.is_const_trait(trait_id))
19731978
}
19741979

19751980
pub fn impl_method_has_trait_impl_trait_tys(self, def_id: DefId) -> bool {

compiler/rustc_middle/src/ty/trait_def.rs

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ pub struct TraitDef {
1818

1919
pub safety: hir::Safety,
2020

21+
/// Whether this trait has been annotated with `#[const_trait]`.
22+
pub constness: hir::Constness,
23+
2124
/// If `true`, then this trait had the `#[rustc_paren_sugar]`
2225
/// attribute, indicating that it should be used with `Foo()`
2326
/// sugar. This is a temporary thing -- eventually any trait will

tests/ui/traits/default-method/rustc_must_implement_one_of_misuse.stderr

+11-11
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,29 @@ LL | struct Struct {}
2323
| ---------------- not a trait
2424

2525
error: function not found in this trait
26-
--> $DIR/rustc_must_implement_one_of_misuse.rs:3:31
26+
--> $DIR/rustc_must_implement_one_of_misuse.rs:8:34
2727
|
2828
LL | #[rustc_must_implement_one_of(a, b)]
29-
| ^
29+
| ^
30+
31+
error: the `#[rustc_must_implement_one_of]` attribute must be used with at least 2 args
32+
--> $DIR/rustc_must_implement_one_of_misuse.rs:14:1
33+
|
34+
LL | #[rustc_must_implement_one_of(a)]
35+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3036

3137
error: function not found in this trait
32-
--> $DIR/rustc_must_implement_one_of_misuse.rs:3:34
38+
--> $DIR/rustc_must_implement_one_of_misuse.rs:3:31
3339
|
3440
LL | #[rustc_must_implement_one_of(a, b)]
35-
| ^
41+
| ^
3642

3743
error: function not found in this trait
38-
--> $DIR/rustc_must_implement_one_of_misuse.rs:8:34
44+
--> $DIR/rustc_must_implement_one_of_misuse.rs:3:34
3945
|
4046
LL | #[rustc_must_implement_one_of(a, b)]
4147
| ^
4248

43-
error: the `#[rustc_must_implement_one_of]` attribute must be used with at least 2 args
44-
--> $DIR/rustc_must_implement_one_of_misuse.rs:14:1
45-
|
46-
LL | #[rustc_must_implement_one_of(a)]
47-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
48-
4949
error: not a function
5050
--> $DIR/rustc_must_implement_one_of_misuse.rs:26:5
5151
|

0 commit comments

Comments
 (0)