Skip to content

Commit 8350528

Browse files
committed
move Constness into TraitPredicate
1 parent 3bcce82 commit 8350528

File tree

49 files changed

+157
-124
lines changed

Some content is hidden

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

49 files changed

+157
-124
lines changed

compiler/rustc_hir/src/hir.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2709,6 +2709,15 @@ pub enum Constness {
27092709
NotConst,
27102710
}
27112711

2712+
impl fmt::Display for Constness {
2713+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2714+
f.write_str(match *self {
2715+
Self::Const => "const",
2716+
Self::NotConst => "non-const",
2717+
})
2718+
}
2719+
}
2720+
27122721
#[derive(Copy, Clone, Encodable, Debug, HashStable_Generic)]
27132722
pub struct FnHeader {
27142723
pub unsafety: Unsafety,

compiler/rustc_infer/src/traits/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ impl Elaborator<'tcx> {
124124

125125
let bound_predicate = obligation.predicate.kind();
126126
match bound_predicate.skip_binder() {
127-
ty::PredicateKind::Trait(data, _) => {
127+
ty::PredicateKind::Trait(data) => {
128128
// Get predicates declared on the trait.
129129
let predicates = tcx.super_predicates_of(data.def_id());
130130

compiler/rustc_lint/src/traits.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl<'tcx> LateLintPass<'tcx> for DropTraitConstraints {
8787
let predicates = cx.tcx.explicit_predicates_of(item.def_id);
8888
for &(predicate, span) in predicates.predicates {
8989
let trait_predicate = match predicate.kind().skip_binder() {
90-
Trait(trait_predicate, _constness) => trait_predicate,
90+
Trait(trait_predicate) => trait_predicate,
9191
_ => continue,
9292
};
9393
let def_id = trait_predicate.trait_ref.def_id;

compiler/rustc_lint/src/unused.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
203203
let mut has_emitted = false;
204204
for &(predicate, _) in cx.tcx.explicit_item_bounds(def) {
205205
// We only look at the `DefId`, so it is safe to skip the binder here.
206-
if let ty::PredicateKind::Trait(ref poly_trait_predicate, _) =
206+
if let ty::PredicateKind::Trait(ref poly_trait_predicate) =
207207
predicate.kind().skip_binder()
208208
{
209209
let def_id = poly_trait_predicate.trait_ref.def_id;

compiler/rustc_middle/src/ty/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2171,7 +2171,7 @@ impl<'tcx> TyCtxt<'tcx> {
21712171
let generic_predicates = self.super_predicates_of(trait_did);
21722172

21732173
for (predicate, _) in generic_predicates.predicates {
2174-
if let ty::PredicateKind::Trait(data, _) = predicate.kind().skip_binder() {
2174+
if let ty::PredicateKind::Trait(data) = predicate.kind().skip_binder() {
21752175
if set.insert(data.def_id()) {
21762176
stack.push(data.def_id());
21772177
}

compiler/rustc_middle/src/ty/error.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ impl<T> ExpectedFound<T> {
3333
#[derive(Clone, Debug, TypeFoldable)]
3434
pub enum TypeError<'tcx> {
3535
Mismatch,
36+
ConstnessMismatch(ExpectedFound<hir::Constness>),
3637
UnsafetyMismatch(ExpectedFound<hir::Unsafety>),
3738
AbiMismatch(ExpectedFound<abi::Abi>),
3839
Mutability,
@@ -106,6 +107,9 @@ impl<'tcx> fmt::Display for TypeError<'tcx> {
106107
CyclicTy(_) => write!(f, "cyclic type of infinite size"),
107108
CyclicConst(_) => write!(f, "encountered a self-referencing constant"),
108109
Mismatch => write!(f, "types differ"),
110+
ConstnessMismatch(values) => {
111+
write!(f, "expected {} fn, found {} fn", values.expected, values.found)
112+
}
109113
UnsafetyMismatch(values) => {
110114
write!(f, "expected {} fn, found {} fn", values.expected, values.found)
111115
}
@@ -213,9 +217,11 @@ impl<'tcx> TypeError<'tcx> {
213217
pub fn must_include_note(&self) -> bool {
214218
use self::TypeError::*;
215219
match self {
216-
CyclicTy(_) | CyclicConst(_) | UnsafetyMismatch(_) | Mismatch | AbiMismatch(_)
217-
| FixedArraySize(_) | ArgumentSorts(..) | Sorts(_) | IntMismatch(_)
218-
| FloatMismatch(_) | VariadicMismatch(_) | TargetFeatureCast(_) => false,
220+
CyclicTy(_) | CyclicConst(_) | UnsafetyMismatch(_) | ConstnessMismatch(_)
221+
| Mismatch | AbiMismatch(_) | FixedArraySize(_) | ArgumentSorts(..) | Sorts(_)
222+
| IntMismatch(_) | FloatMismatch(_) | VariadicMismatch(_) | TargetFeatureCast(_) => {
223+
false
224+
}
219225

220226
Mutability
221227
| ArgumentMutability(_)

compiler/rustc_middle/src/ty/flags.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ impl FlagComputation {
216216

217217
fn add_predicate_atom(&mut self, atom: ty::PredicateKind<'_>) {
218218
match atom {
219-
ty::PredicateKind::Trait(trait_pred, _constness) => {
219+
ty::PredicateKind::Trait(trait_pred) => {
220220
self.add_substs(trait_pred.trait_ref.substs);
221221
}
222222
ty::PredicateKind::RegionOutlives(ty::OutlivesPredicate(a, b)) => {

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ pub enum PredicateKind<'tcx> {
456456
/// A trait predicate will have `Constness::Const` if it originates
457457
/// from a bound on a `const fn` without the `?const` opt-out (e.g.,
458458
/// `const fn foobar<Foo: Bar>() {}`).
459-
Trait(TraitPredicate<'tcx>, Constness),
459+
Trait(TraitPredicate<'tcx>),
460460

461461
/// `where 'a: 'b`
462462
RegionOutlives(RegionOutlivesPredicate<'tcx>),
@@ -612,6 +612,11 @@ impl<'tcx> Predicate<'tcx> {
612612
#[derive(HashStable, TypeFoldable)]
613613
pub struct TraitPredicate<'tcx> {
614614
pub trait_ref: TraitRef<'tcx>,
615+
616+
/// A trait predicate will have `Constness::Const` if it originates
617+
/// from a bound on a `const fn` without the `?const` opt-out (e.g.,
618+
/// `const fn foobar<Foo: Bar>() {}`).
619+
pub constness: hir::Constness,
615620
}
616621

617622
pub type PolyTraitPredicate<'tcx> = ty::Binder<'tcx, TraitPredicate<'tcx>>;
@@ -745,24 +750,27 @@ impl ToPredicate<'tcx> for PredicateKind<'tcx> {
745750

746751
impl<'tcx> ToPredicate<'tcx> for ConstnessAnd<TraitRef<'tcx>> {
747752
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
748-
PredicateKind::Trait(ty::TraitPredicate { trait_ref: self.value }, self.constness)
749-
.to_predicate(tcx)
753+
PredicateKind::Trait(ty::TraitPredicate {
754+
trait_ref: self.value,
755+
constness: self.constness,
756+
})
757+
.to_predicate(tcx)
750758
}
751759
}
752760

753761
impl<'tcx> ToPredicate<'tcx> for ConstnessAnd<PolyTraitRef<'tcx>> {
754762
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
755763
self.value
756764
.map_bound(|trait_ref| {
757-
PredicateKind::Trait(ty::TraitPredicate { trait_ref }, self.constness)
765+
PredicateKind::Trait(ty::TraitPredicate { trait_ref, constness: self.constness })
758766
})
759767
.to_predicate(tcx)
760768
}
761769
}
762770

763-
impl<'tcx> ToPredicate<'tcx> for ConstnessAnd<PolyTraitPredicate<'tcx>> {
771+
impl<'tcx> ToPredicate<'tcx> for PolyTraitPredicate<'tcx> {
764772
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
765-
self.value.map_bound(|value| PredicateKind::Trait(value, self.constness)).to_predicate(tcx)
773+
self.map_bound(PredicateKind::Trait).to_predicate(tcx)
766774
}
767775
}
768776

@@ -788,8 +796,8 @@ impl<'tcx> Predicate<'tcx> {
788796
pub fn to_opt_poly_trait_ref(self) -> Option<ConstnessAnd<PolyTraitRef<'tcx>>> {
789797
let predicate = self.kind();
790798
match predicate.skip_binder() {
791-
PredicateKind::Trait(t, constness) => {
792-
Some(ConstnessAnd { constness, value: predicate.rebind(t.trait_ref) })
799+
PredicateKind::Trait(t) => {
800+
Some(ConstnessAnd { constness: t.constness, value: predicate.rebind(t.trait_ref) })
793801
}
794802
PredicateKind::Projection(..)
795803
| PredicateKind::Subtype(..)

compiler/rustc_middle/src/ty/print/pretty.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ pub trait PrettyPrinter<'tcx>:
630630
for (predicate, _) in bounds {
631631
let predicate = predicate.subst(self.tcx(), substs);
632632
let bound_predicate = predicate.kind();
633-
if let ty::PredicateKind::Trait(pred, _) = bound_predicate.skip_binder() {
633+
if let ty::PredicateKind::Trait(pred) = bound_predicate.skip_binder() {
634634
let trait_ref = bound_predicate.rebind(pred.trait_ref);
635635
// Don't print +Sized, but rather +?Sized if absent.
636636
if Some(trait_ref.def_id()) == self.tcx().lang_items().sized_trait() {
@@ -2264,10 +2264,7 @@ define_print_and_forward_display! {
22642264

22652265
ty::PredicateKind<'tcx> {
22662266
match *self {
2267-
ty::PredicateKind::Trait(ref data, constness) => {
2268-
if let hir::Constness::Const = constness {
2269-
p!("const ");
2270-
}
2267+
ty::PredicateKind::Trait(ref data) => {
22712268
p!(print(data))
22722269
}
22732270
ty::PredicateKind::Subtype(predicate) => p!(print(predicate)),

compiler/rustc_middle/src/ty/relate.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,20 @@ impl<'tcx> Relate<'tcx> for ty::FnSig<'tcx> {
200200
}
201201
}
202202

203+
impl<'tcx> Relate<'tcx> for ast::Constness {
204+
fn relate<R: TypeRelation<'tcx>>(
205+
relation: &mut R,
206+
a: ast::Constness,
207+
b: ast::Constness,
208+
) -> RelateResult<'tcx, ast::Constness> {
209+
if a != b {
210+
Err(TypeError::ConstnessMismatch(expected_found(relation, a, b)))
211+
} else {
212+
Ok(a)
213+
}
214+
}
215+
}
216+
203217
impl<'tcx> Relate<'tcx> for ast::Unsafety {
204218
fn relate<R: TypeRelation<'tcx>>(
205219
relation: &mut R,
@@ -767,7 +781,10 @@ impl<'tcx> Relate<'tcx> for ty::TraitPredicate<'tcx> {
767781
a: ty::TraitPredicate<'tcx>,
768782
b: ty::TraitPredicate<'tcx>,
769783
) -> RelateResult<'tcx, ty::TraitPredicate<'tcx>> {
770-
Ok(ty::TraitPredicate { trait_ref: relation.relate(a.trait_ref, b.trait_ref)? })
784+
Ok(ty::TraitPredicate {
785+
trait_ref: relation.relate(a.trait_ref, b.trait_ref)?,
786+
constness: relation.relate(a.constness, b.constness)?,
787+
})
771788
}
772789
}
773790

0 commit comments

Comments
 (0)