Skip to content

Commit

Permalink
Auto merge of rust-lang#127200 - fee1-dead-contrib:trait_def_const_tr…
Browse files Browse the repository at this point in the history
…ait, r=<try>

Add `constness` to `TraitDef`

Second attempt at fixing the regression @ rust-lang#120639 (comment)

r? project-const-traits
  • Loading branch information
bors committed Jul 1, 2024
2 parents f92a6c4 + ee2d52a commit 6cb3b57
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 26 deletions.
9 changes: 6 additions & 3 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -625,9 +625,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
_ => Const::No,
}
} else {
self.tcx
.get_attr(def_id, sym::const_trait)
.map_or(Const::No, |attr| Const::Yes(attr.span))
if self.tcx.is_const_trait(def_id) {
// FIXME(effects) span
Const::Yes(self.tcx.def_ident_span(def_id).unwrap())
} else {
Const::No
}
}
} else {
Const::No
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl<'tcx> Bounds<'tcx> {
}

(_, 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;
}
tcx.consts.true_
Expand Down
8 changes: 7 additions & 1 deletion compiler/rustc_hir_analysis/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
};

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);
if paren_sugar && !tcx.features().unboxed_closures {
tcx.dcx().emit_err(errors::ParenSugarAttribute { span: item.span });
Expand Down Expand Up @@ -1348,6 +1353,7 @@ fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef {
ty::TraitDef {
def_id: def_id.to_def_id(),
safety,
constness,
paren_sugar,
has_auto_impl: is_auto,
is_marker,
Expand Down Expand Up @@ -1680,7 +1686,7 @@ fn check_impl_constness(
}

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;
}

Expand Down
10 changes: 2 additions & 8 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
}
if let ty::BoundConstness::Const | ty::BoundConstness::ConstIfConst = constness
&& 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 {
span,
Expand Down Expand Up @@ -1847,19 +1847,13 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
path.segments[..path.segments.len() - 2].iter(),
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(
span,
opt_self_ty,
def_id,
&path.segments[path.segments.len() - 2],
path.segments.last().unwrap(),
constness,
ty::BoundConstness::NotConst,
)
}
Res::PrimTy(prim_ty) => {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/method/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// FIXME(effects) find a better way to do this
// Operators don't have generic methods, but making them `#[const_trait]` gives them
// `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(
args.iter()
.chain([self.tcx.expected_host_effect_param_for_body(self.body_id).into()]),
Expand Down
7 changes: 6 additions & 1 deletion compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1966,9 +1966,14 @@ impl<'tcx> TyCtxt<'tcx> {
) && 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]
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 {
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_middle/src/ty/trait_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ pub struct TraitDef {

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]`
/// attribute, indicating that it should be used with `Foo()`
/// sugar. This is a temporary thing -- eventually any trait will
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,29 @@ LL | struct Struct {}
| ---------------- not a trait

error: function not found in this trait
--> $DIR/rustc_must_implement_one_of_misuse.rs:3:31
--> $DIR/rustc_must_implement_one_of_misuse.rs:8:34
|
LL | #[rustc_must_implement_one_of(a, b)]
| ^
| ^

error: the `#[rustc_must_implement_one_of]` attribute must be used with at least 2 args
--> $DIR/rustc_must_implement_one_of_misuse.rs:14:1
|
LL | #[rustc_must_implement_one_of(a)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: function not found in this trait
--> $DIR/rustc_must_implement_one_of_misuse.rs:3:34
--> $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:8:34
--> $DIR/rustc_must_implement_one_of_misuse.rs:3:34
|
LL | #[rustc_must_implement_one_of(a, b)]
| ^

error: the `#[rustc_must_implement_one_of]` attribute must be used with at least 2 args
--> $DIR/rustc_must_implement_one_of_misuse.rs:14:1
|
LL | #[rustc_must_implement_one_of(a)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: not a function
--> $DIR/rustc_must_implement_one_of_misuse.rs:26:5
|
Expand Down

0 comments on commit 6cb3b57

Please sign in to comment.