Skip to content

Use constness query for const_trait, too #134114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion compiler/rustc_const_eval/src/const_eval/fn_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use rustc_hir::def::DefKind;
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_middle::query::Providers;
use rustc_middle::ty::TyCtxt;
use rustc_span::sym;

pub fn is_parent_const_impl_raw(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
fn is_parent_const_impl_raw(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
let parent_id = tcx.local_parent(def_id);
matches!(tcx.def_kind(parent_id), DefKind::Impl { .. })
&& tcx.constness(parent_id) == hir::Constness::Const
Expand All @@ -25,6 +26,14 @@ fn constness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::Constness {
hir::Constness::Const
}
hir::Node::Item(hir::Item { kind: hir::ItemKind::Impl(impl_), .. }) => impl_.constness,
hir::Node::Item(hir::Item { kind: hir::ItemKind::Trait(..), .. }) => {
if tcx.has_attr(def_id, sym::const_trait) {
hir::Constness::Const
} else {
hir::Constness::NotConst
}
}

hir::Node::ForeignItem(_) => {
// Foreign items cannot be evaluated at compile-time.
hir::Constness::NotConst
Expand Down
8 changes: 0 additions & 8 deletions compiler/rustc_hir_analysis/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1135,13 +1135,6 @@ fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef {
_ => span_bug!(item.span, "trait_def_of_item invoked on non-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 });
Expand Down Expand Up @@ -1299,7 +1292,6 @@ 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
2 changes: 1 addition & 1 deletion compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1269,6 +1269,7 @@ fn should_encode_constness(def_kind: DefKind) -> bool {
| DefKind::Closure
| DefKind::Impl { of_trait: true }
| DefKind::Variant
| DefKind::Trait
| DefKind::Ctor(..) => true,

DefKind::Struct
Expand All @@ -1287,7 +1288,6 @@ fn should_encode_constness(def_kind: DefKind) -> bool {
| DefKind::InlineConst
| DefKind::AssocTy
| DefKind::TyParam
| DefKind::Trait
| DefKind::TraitAlias
| DefKind::Mod
| DefKind::ForeignMod
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2067,7 +2067,8 @@ impl<'tcx> TyCtxt<'tcx> {

#[inline]
pub fn is_const_trait(self, def_id: DefId) -> bool {
self.trait_def(def_id).constness == hir::Constness::Const
debug_assert_eq!(self.def_kind(def_id), DefKind::Trait);
self.constness(def_id) == hir::Constness::Const
}

#[inline]
Expand Down
3 changes: 0 additions & 3 deletions compiler/rustc_middle/src/ty/trait_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ 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
Loading