Auto merge of #127200 - fee1-dead-contrib:trait_def_const_trait, r=compiler-errors

Add `constness` to `TraitDef`

Second attempt at fixing the regression @ https://github.com/rust-lang/rust/pull/120639#issuecomment-2198373716

r? project-const-traits
This commit is contained in:
bors 2024-07-09 06:51:35 +00:00
commit 5be2ec7245
9 changed files with 42 additions and 31 deletions

View File

@ -625,9 +625,12 @@ fn lower_assoc_item(
_ => Const::No, _ => Const::No,
} }
} else { } else {
self.tcx if self.tcx.is_const_trait(def_id) {
.get_attr(def_id, sym::const_trait) // FIXME(effects) span
.map_or(Const::No, |attr| Const::Yes(attr.span)) Const::Yes(self.tcx.def_ident_span(def_id).unwrap())
} else {
Const::No
}
} }
} else { } else {
Const::No Const::No

View File

@ -556,7 +556,7 @@ pub struct BuiltinAttribute {
), ),
// RFC 2632 // RFC 2632
gated!( gated!(
const_trait, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::Yes, const_trait_impl, const_trait, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::No, const_trait_impl,
"`const_trait` is a temporary placeholder for marking a trait that is suitable for `const` \ "`const_trait` is a temporary placeholder for marking a trait that is suitable for `const` \
`impls` and all default bodies as `const`, which may be removed or renamed in the \ `impls` and all default bodies as `const`, which may be removed or renamed in the \
future." future."

View File

@ -7,7 +7,7 @@
use rustc_middle::ty::fold::FnMutDelegate; use rustc_middle::ty::fold::FnMutDelegate;
use rustc_middle::ty::{self, Ty, TyCtxt, Upcast}; use rustc_middle::ty::{self, Ty, TyCtxt, Upcast};
use rustc_span::def_id::DefId; use rustc_span::def_id::DefId;
use rustc_span::{sym, Span}; use rustc_span::Span;
/// Collects together a list of type bounds. These lists of bounds occur in many places /// Collects together a list of type bounds. These lists of bounds occur in many places
/// in Rust's syntax: /// in Rust's syntax:
@ -80,7 +80,7 @@ pub fn push_trait_bound(
} }
(_, ty::BoundConstness::NotConst) => { (_, ty::BoundConstness::NotConst) => {
if !tcx.has_attr(bound_trait_ref.def_id(), sym::const_trait) { if !tcx.is_const_trait(bound_trait_ref.def_id()) {
return; return;
} }
tcx.consts.true_ tcx.consts.true_

View File

@ -1194,6 +1194,11 @@ fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef {
_ => span_bug!(item.span, "trait_def_of_item invoked on non-trait"), _ => span_bug!(item.span, "trait_def_of_item invoked on non-trait"),
}; };
let constness = if 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); let paren_sugar = tcx.has_attr(def_id, sym::rustc_paren_sugar);
if paren_sugar && !tcx.features().unboxed_closures { if paren_sugar && !tcx.features().unboxed_closures {
tcx.dcx().emit_err(errors::ParenSugarAttribute { span: item.span }); tcx.dcx().emit_err(errors::ParenSugarAttribute { span: item.span });
@ -1349,6 +1354,7 @@ fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef {
ty::TraitDef { ty::TraitDef {
def_id: def_id.to_def_id(), def_id: def_id.to_def_id(),
safety, safety,
constness,
paren_sugar, paren_sugar,
has_auto_impl: is_auto, has_auto_impl: is_auto,
is_marker, is_marker,
@ -1682,7 +1688,7 @@ fn check_impl_constness(
} }
let trait_def_id = hir_trait_ref.trait_def_id()?; let trait_def_id = hir_trait_ref.trait_def_id()?;
if tcx.has_attr(trait_def_id, sym::const_trait) { if tcx.is_const_trait(trait_def_id) {
return None; return None;
} }

View File

@ -47,7 +47,7 @@
use rustc_session::lint::builtin::AMBIGUOUS_ASSOCIATED_ITEMS; use rustc_session::lint::builtin::AMBIGUOUS_ASSOCIATED_ITEMS;
use rustc_span::edit_distance::find_best_match_for_name; use rustc_span::edit_distance::find_best_match_for_name;
use rustc_span::symbol::{kw, Ident, Symbol}; use rustc_span::symbol::{kw, Ident, Symbol};
use rustc_span::{sym, Span, DUMMY_SP}; use rustc_span::{Span, DUMMY_SP};
use rustc_target::spec::abi; use rustc_target::spec::abi;
use rustc_trait_selection::infer::InferCtxtExt; use rustc_trait_selection::infer::InferCtxtExt;
use rustc_trait_selection::traits::wf::object_region_bounds; use rustc_trait_selection::traits::wf::object_region_bounds;
@ -560,7 +560,7 @@ fn inferred_kind(
} }
if let ty::BoundConstness::Const | ty::BoundConstness::ConstIfConst = constness if let ty::BoundConstness::Const | ty::BoundConstness::ConstIfConst = constness
&& generics.has_self && generics.has_self
&& !tcx.has_attr(def_id, sym::const_trait) && !tcx.is_const_trait(def_id)
{ {
let reported = tcx.dcx().emit_err(crate::errors::ConstBoundForNonConstTrait { let reported = tcx.dcx().emit_err(crate::errors::ConstBoundForNonConstTrait {
span, span,
@ -1848,19 +1848,13 @@ pub fn lower_path(
path.segments[..path.segments.len() - 2].iter(), path.segments[..path.segments.len() - 2].iter(),
GenericsArgsErrExtend::None, GenericsArgsErrExtend::None,
); );
// HACK: until we support `<Type as ~const Trait>`, assume all of them are.
let constness = if tcx.has_attr(tcx.parent(def_id), sym::const_trait) {
ty::BoundConstness::ConstIfConst
} else {
ty::BoundConstness::NotConst
};
self.lower_qpath( self.lower_qpath(
span, span,
opt_self_ty, opt_self_ty,
def_id, def_id,
&path.segments[path.segments.len() - 2], &path.segments[path.segments.len() - 2],
path.segments.last().unwrap(), path.segments.last().unwrap(),
constness, ty::BoundConstness::NotConst,
) )
} }
Res::PrimTy(prim_ty) => { Res::PrimTy(prim_ty) => {

View File

@ -21,7 +21,7 @@
use rustc_middle::ty::{GenericArgs, GenericArgsRef}; use rustc_middle::ty::{GenericArgs, GenericArgsRef};
use rustc_middle::{bug, span_bug}; use rustc_middle::{bug, span_bug};
use rustc_span::symbol::Ident; use rustc_span::symbol::Ident;
use rustc_span::{sym, Span}; use rustc_span::Span;
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt; use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
use rustc_trait_selection::traits::{self, NormalizeExt}; use rustc_trait_selection::traits::{self, NormalizeExt};
@ -359,7 +359,7 @@ pub(super) fn lookup_method_in_trait(
// FIXME(effects) find a better way to do this // FIXME(effects) find a better way to do this
// Operators don't have generic methods, but making them `#[const_trait]` gives them // Operators don't have generic methods, but making them `#[const_trait]` gives them
// `const host: bool`. // `const host: bool`.
let args = if self.tcx.has_attr(trait_def_id, sym::const_trait) { let args = if self.tcx.is_const_trait(trait_def_id) {
self.tcx.mk_args_from_iter( self.tcx.mk_args_from_iter(
args.iter() args.iter()
.chain([self.tcx.expected_host_effect_param_for_body(self.body_id).into()]), .chain([self.tcx.expected_host_effect_param_for_body(self.body_id).into()]),

View File

@ -1967,9 +1967,14 @@ pub fn is_const_fn_raw(self, def_id: DefId) -> bool {
) && self.constness(def_id) == hir::Constness::Const ) && self.constness(def_id) == hir::Constness::Const
} }
#[inline]
pub fn is_const_trait(self, def_id: DefId) -> bool {
self.trait_def(def_id).constness == hir::Constness::Const
}
#[inline] #[inline]
pub fn is_const_default_method(self, def_id: DefId) -> bool { pub fn is_const_default_method(self, def_id: DefId) -> bool {
matches!(self.trait_of_item(def_id), Some(trait_id) if self.has_attr(trait_id, sym::const_trait)) matches!(self.trait_of_item(def_id), Some(trait_id) if self.is_const_trait(trait_id))
} }
pub fn impl_method_has_trait_impl_trait_tys(self, def_id: DefId) -> bool { pub fn impl_method_has_trait_impl_trait_tys(self, def_id: DefId) -> bool {

View File

@ -18,6 +18,9 @@ pub struct TraitDef {
pub safety: hir::Safety, pub safety: hir::Safety,
/// Whether this trait has been annotated with `#[const_trait]`.
pub constness: hir::Constness,
/// If `true`, then this trait had the `#[rustc_paren_sugar]` /// If `true`, then this trait had the `#[rustc_paren_sugar]`
/// attribute, indicating that it should be used with `Foo()` /// attribute, indicating that it should be used with `Foo()`
/// sugar. This is a temporary thing -- eventually any trait will /// sugar. This is a temporary thing -- eventually any trait will

View File

@ -22,18 +22,6 @@ LL |
LL | struct Struct {} LL | struct Struct {}
| ---------------- not a trait | ---------------- not a trait
error: function not found in this trait
--> $DIR/rustc_must_implement_one_of_misuse.rs:3:31
|
LL | #[rustc_must_implement_one_of(a, b)]
| ^
error: function not found in this trait
--> $DIR/rustc_must_implement_one_of_misuse.rs:3:34
|
LL | #[rustc_must_implement_one_of(a, b)]
| ^
error: function not found in this trait error: function not found in this trait
--> $DIR/rustc_must_implement_one_of_misuse.rs:8:34 --> $DIR/rustc_must_implement_one_of_misuse.rs:8:34
| |
@ -46,6 +34,18 @@ error: the `#[rustc_must_implement_one_of]` attribute must be used with at least
LL | #[rustc_must_implement_one_of(a)] LL | #[rustc_must_implement_one_of(a)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: function not found in this trait
--> $DIR/rustc_must_implement_one_of_misuse.rs:3:31
|
LL | #[rustc_must_implement_one_of(a, b)]
| ^
error: function not found in this trait
--> $DIR/rustc_must_implement_one_of_misuse.rs:3:34
|
LL | #[rustc_must_implement_one_of(a, b)]
| ^
error: not a function error: not a function
--> $DIR/rustc_must_implement_one_of_misuse.rs:26:5 --> $DIR/rustc_must_implement_one_of_misuse.rs:26:5
| |