Skip to content

Commit 136eaa1

Browse files
committedAug 14, 2021
Auto merge of #87375 - fee1-dead:move-constness-to-traitpred, r=oli-obk
Try filtering out non-const impls when we expect const impls **TL;DR**: Associated types on const impls are now bounded; we now disallow calling a const function with bounds when the specified type param only has a non-const impl. r? `@oli-obk`
2 parents fa26929 + 74627c1 commit 136eaa1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+517
-211
lines changed
 

‎compiler/rustc_hir/src/hir.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2751,6 +2751,15 @@ pub enum Constness {
27512751
NotConst,
27522752
}
27532753

2754+
impl fmt::Display for Constness {
2755+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2756+
f.write_str(match *self {
2757+
Self::Const => "const",
2758+
Self::NotConst => "non-const",
2759+
})
2760+
}
2761+
}
2762+
27542763
#[derive(Copy, Clone, Encodable, Debug, HashStable_Generic)]
27552764
pub struct FnHeader {
27562765
pub unsafety: Unsafety,
@@ -3252,8 +3261,13 @@ impl<'hir> Node<'hir> {
32523261
}
32533262
}
32543263

3255-
/// Returns `Constness::Const` when this node is a const fn/impl.
3256-
pub fn constness(&self) -> Constness {
3264+
/// Returns `Constness::Const` when this node is a const fn/impl/item,
3265+
///
3266+
/// HACK(fee1-dead): or an associated type in a trait. This works because
3267+
/// only typeck cares about const trait predicates, so although the predicates
3268+
/// query would return const predicates when it does not need to be const,
3269+
/// it wouldn't have any effect.
3270+
pub fn constness_for_typeck(&self) -> Constness {
32573271
match self {
32583272
Node::Item(Item {
32593273
kind: ItemKind::Fn(FnSig { header: FnHeader { constness, .. }, .. }, ..),
@@ -3269,6 +3283,11 @@ impl<'hir> Node<'hir> {
32693283
})
32703284
| Node::Item(Item { kind: ItemKind::Impl(Impl { constness, .. }), .. }) => *constness,
32713285

3286+
Node::Item(Item { kind: ItemKind::Const(..), .. })
3287+
| Node::TraitItem(TraitItem { kind: TraitItemKind::Const(..), .. })
3288+
| Node::TraitItem(TraitItem { kind: TraitItemKind::Type(..), .. })
3289+
| Node::ImplItem(ImplItem { kind: ImplItemKind::Const(..), .. }) => Constness::Const,
3290+
32723291
_ => Constness::NotConst,
32733292
}
32743293
}

‎compiler/rustc_infer/src/traits/engine.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::infer::InferCtxt;
22
use crate::traits::Obligation;
3+
use rustc_hir as hir;
34
use rustc_hir::def_id::DefId;
45
use rustc_middle::ty::{self, ToPredicate, Ty, WithConstness};
56

@@ -49,11 +50,28 @@ pub trait TraitEngine<'tcx>: 'tcx {
4950
infcx: &InferCtxt<'_, 'tcx>,
5051
) -> Result<(), Vec<FulfillmentError<'tcx>>>;
5152

53+
fn select_all_with_constness_or_error(
54+
&mut self,
55+
infcx: &InferCtxt<'_, 'tcx>,
56+
_constness: hir::Constness,
57+
) -> Result<(), Vec<FulfillmentError<'tcx>>> {
58+
self.select_all_or_error(infcx)
59+
}
60+
5261
fn select_where_possible(
5362
&mut self,
5463
infcx: &InferCtxt<'_, 'tcx>,
5564
) -> Result<(), Vec<FulfillmentError<'tcx>>>;
5665

66+
// FIXME this should not provide a default body for chalk as chalk should be updated
67+
fn select_with_constness_where_possible(
68+
&mut self,
69+
infcx: &InferCtxt<'_, 'tcx>,
70+
_constness: hir::Constness,
71+
) -> Result<(), Vec<FulfillmentError<'tcx>>> {
72+
self.select_where_possible(infcx)
73+
}
74+
5775
fn pending_obligations(&self) -> Vec<PredicateObligation<'tcx>>;
5876
}
5977

0 commit comments

Comments
 (0)