Skip to content

Commit

Permalink
Recognize bounds on impls as const bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
fee1-dead committed Jul 19, 2021
1 parent d05a286 commit 4b82bbe
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
21 changes: 21 additions & 0 deletions compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3064,6 +3064,27 @@ impl<'hir> Node<'hir> {
Node::Crate(_) | Node::Visibility(_) => None,
}
}

/// Returns `Constness::Const` when this node is a const fn/impl.
pub fn constness(&self) -> Constness {
match self {
Node::Item(Item {
kind: ItemKind::Fn(FnSig { header: FnHeader { constness, .. }, .. }, ..),
..
})
| Node::TraitItem(TraitItem {
kind: TraitItemKind::Fn(FnSig { header: FnHeader { constness, .. }, .. }, ..),
..
})
| Node::ImplItem(ImplItem {
kind: ImplItemKind::Fn(FnSig { header: FnHeader { constness, .. }, .. }, ..),
..
})
| Node::Item(Item { kind: ItemKind::Impl(Impl { constness, .. }), .. }) => *constness,

_ => Constness::NotConst,
}
}
}

// Some nodes are used a lot. Make sure they don't unintentionally get bigger.
Expand Down
9 changes: 1 addition & 8 deletions compiler/rustc_typeck/src/check/fn_ctxt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use rustc_hir::def_id::DefId;
use rustc_infer::infer;
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
use rustc_infer::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind};
use rustc_middle::hir::map::blocks::FnLikeNode;
use rustc_middle::ty::fold::TypeFoldable;
use rustc_middle::ty::subst::GenericArgKind;
use rustc_middle::ty::{self, Const, Ty, TyCtxt};
Expand Down Expand Up @@ -175,13 +174,7 @@ impl<'a, 'tcx> AstConv<'tcx> for FnCtxt<'a, 'tcx> {
}

fn default_constness_for_trait_bounds(&self) -> hir::Constness {
// FIXME: refactor this into a method
let node = self.tcx.hir().get(self.body_id);
if let Some(fn_like) = FnLikeNode::from_node(node) {
fn_like.constness()
} else {
hir::Constness::NotConst
}
self.tcx.hir().get(self.body_id).constness()
}

fn get_type_parameter_bounds(
Expand Down
7 changes: 1 addition & 6 deletions compiler/rustc_typeck/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ use rustc_hir::def_id::{DefId, LocalDefId, LOCAL_CRATE};
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
use rustc_hir::weak_lang_items;
use rustc_hir::{GenericParamKind, HirId, Node};
use rustc_middle::hir::map::blocks::FnLikeNode;
use rustc_middle::hir::map::Map;
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
use rustc_middle::mir::mono::Linkage;
Expand Down Expand Up @@ -358,11 +357,7 @@ impl AstConv<'tcx> for ItemCtxt<'tcx> {
}

fn default_constness_for_trait_bounds(&self) -> hir::Constness {
if let Some(fn_like) = FnLikeNode::from_node(self.node()) {
fn_like.constness()
} else {
hir::Constness::NotConst
}
self.node().constness()
}

fn get_type_parameter_bounds(
Expand Down
15 changes: 15 additions & 0 deletions src/test/ui/rfc-2632-const-trait-impl/call-generic-in-impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// check-pass
#![feature(const_fn_trait_bound)]
#![feature(const_trait_impl)]

trait MyPartialEq {
fn eq(&self, other: &Self) -> bool;
}

impl<T: PartialEq> const MyPartialEq for T {
fn eq(&self, other: &Self) -> bool {
PartialEq::eq(self, other)
}
}

fn main() {}

0 comments on commit 4b82bbe

Please sign in to comment.