Skip to content

Commit 4b82bbe

Browse files
committed
Recognize bounds on impls as const bounds
1 parent d05a286 commit 4b82bbe

File tree

4 files changed

+38
-14
lines changed

4 files changed

+38
-14
lines changed

compiler/rustc_hir/src/hir.rs

+21
Original file line numberDiff line numberDiff line change
@@ -3064,6 +3064,27 @@ impl<'hir> Node<'hir> {
30643064
Node::Crate(_) | Node::Visibility(_) => None,
30653065
}
30663066
}
3067+
3068+
/// Returns `Constness::Const` when this node is a const fn/impl.
3069+
pub fn constness(&self) -> Constness {
3070+
match self {
3071+
Node::Item(Item {
3072+
kind: ItemKind::Fn(FnSig { header: FnHeader { constness, .. }, .. }, ..),
3073+
..
3074+
})
3075+
| Node::TraitItem(TraitItem {
3076+
kind: TraitItemKind::Fn(FnSig { header: FnHeader { constness, .. }, .. }, ..),
3077+
..
3078+
})
3079+
| Node::ImplItem(ImplItem {
3080+
kind: ImplItemKind::Fn(FnSig { header: FnHeader { constness, .. }, .. }, ..),
3081+
..
3082+
})
3083+
| Node::Item(Item { kind: ItemKind::Impl(Impl { constness, .. }), .. }) => *constness,
3084+
3085+
_ => Constness::NotConst,
3086+
}
3087+
}
30673088
}
30683089

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

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

+1-8
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

+1-6
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(
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)