Skip to content

Commit 1008ace

Browse files
Rollup merge of #87273 - fee1-dead:impl-const-impl-bounds, r=oli-obk
Recognize bounds on impls as const bounds r? ```@oli-obk```
2 parents e6380a6 + 4b82bbe commit 1008ace

File tree

5 files changed

+49
-22
lines changed

5 files changed

+49
-22
lines changed

compiler/rustc_hir/src/hir.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3060,6 +3060,27 @@ impl<'hir> Node<'hir> {
30603060
Node::Crate(_) | Node::Visibility(_) => None,
30613061
}
30623062
}
3063+
3064+
/// Returns `Constness::Const` when this node is a const fn/impl.
3065+
pub fn constness(&self) -> Constness {
3066+
match self {
3067+
Node::Item(Item {
3068+
kind: ItemKind::Fn(FnSig { header: FnHeader { constness, .. }, .. }, ..),
3069+
..
3070+
})
3071+
| Node::TraitItem(TraitItem {
3072+
kind: TraitItemKind::Fn(FnSig { header: FnHeader { constness, .. }, .. }, ..),
3073+
..
3074+
})
3075+
| Node::ImplItem(ImplItem {
3076+
kind: ImplItemKind::Fn(FnSig { header: FnHeader { constness, .. }, .. }, ..),
3077+
..
3078+
})
3079+
| Node::Item(Item { kind: ItemKind::Impl(Impl { constness, .. }), .. }) => *constness,
3080+
3081+
_ => Constness::NotConst,
3082+
}
3083+
}
30633084
}
30643085

30653086
// Some nodes are used a lot. Make sure they don't unintentionally get bigger.

compiler/rustc_mir/src/transform/check_consts/validation.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -897,16 +897,19 @@ impl Visitor<'tcx> for Validator<'mir, 'tcx> {
897897
permitted = true;
898898
}
899899
}
900-
let mut const_impls = true;
901-
tcx.for_each_relevant_impl(trait_id, substs.type_at(0), |imp| {
902-
if const_impls {
903-
if let hir::Constness::NotConst = tcx.impl_constness(imp) {
904-
const_impls = false;
900+
if !permitted {
901+
// if trait's impls are all const, permit the call.
902+
let mut const_impls = true;
903+
tcx.for_each_relevant_impl(trait_id, substs.type_at(0), |imp| {
904+
if const_impls {
905+
if let hir::Constness::NotConst = tcx.impl_constness(imp) {
906+
const_impls = false;
907+
}
905908
}
909+
});
910+
if const_impls {
911+
permitted = true;
906912
}
907-
});
908-
if const_impls {
909-
permitted = true;
910913
}
911914
}
912915

compiler/rustc_typeck/src/check/fn_ctxt/mod.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use rustc_hir::def_id::DefId;
1515
use rustc_infer::infer;
1616
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
1717
use rustc_infer::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind};
18-
use rustc_middle::hir::map::blocks::FnLikeNode;
1918
use rustc_middle::ty::fold::TypeFoldable;
2019
use rustc_middle::ty::subst::GenericArgKind;
2120
use rustc_middle::ty::{self, Const, Ty, TyCtxt};
@@ -175,13 +174,7 @@ impl<'a, 'tcx> AstConv<'tcx> for FnCtxt<'a, 'tcx> {
175174
}
176175

177176
fn default_constness_for_trait_bounds(&self) -> hir::Constness {
178-
// FIXME: refactor this into a method
179-
let node = self.tcx.hir().get(self.body_id);
180-
if let Some(fn_like) = FnLikeNode::from_node(node) {
181-
fn_like.constness()
182-
} else {
183-
hir::Constness::NotConst
184-
}
177+
self.tcx.hir().get(self.body_id).constness()
185178
}
186179

187180
fn get_type_parameter_bounds(

compiler/rustc_typeck/src/collect.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ use rustc_hir::def_id::{DefId, LocalDefId, LOCAL_CRATE};
3535
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
3636
use rustc_hir::weak_lang_items;
3737
use rustc_hir::{GenericParamKind, HirId, Node};
38-
use rustc_middle::hir::map::blocks::FnLikeNode;
3938
use rustc_middle::hir::map::Map;
4039
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
4140
use rustc_middle::mir::mono::Linkage;
@@ -358,11 +357,7 @@ impl AstConv<'tcx> for ItemCtxt<'tcx> {
358357
}
359358

360359
fn default_constness_for_trait_bounds(&self) -> hir::Constness {
361-
if let Some(fn_like) = FnLikeNode::from_node(self.node()) {
362-
fn_like.constness()
363-
} else {
364-
hir::Constness::NotConst
365-
}
360+
self.node().constness()
366361
}
367362

368363
fn get_type_parameter_bounds(
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// check-pass
2+
#![feature(const_fn_trait_bound)]
3+
#![feature(const_trait_impl)]
4+
5+
trait MyPartialEq {
6+
fn eq(&self, other: &Self) -> bool;
7+
}
8+
9+
impl<T: PartialEq> const MyPartialEq for T {
10+
fn eq(&self, other: &Self) -> bool {
11+
PartialEq::eq(self, other)
12+
}
13+
}
14+
15+
fn main() {}

0 commit comments

Comments
 (0)