Skip to content

Commit ad467ae

Browse files
committed
More attempts to fix ui errors
1 parent cbb2cf8 commit ad467ae

File tree

17 files changed

+153
-21
lines changed

17 files changed

+153
-21
lines changed

compiler/rustc_const_eval/src/transform/check_consts/check.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
736736
});
737737

738738
match implsrc {
739-
Ok(Some(ImplSource::Param(_, ty::ConstnessArg::Required))) => {
739+
Ok(Some(ImplSource::Param(_, ty::ConstnessArg::Required | ty::ConstnessArg::Param))) => {
740740
debug!(
741741
"const_trait_impl: provided {:?} via where-clause in {:?}",
742742
trait_ref, param_env

compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ impl Qualif for NeedsNonConstDrop {
161161
ty::Binder::dummy(ty::TraitPredicate {
162162
trait_ref: ty::TraitRef {
163163
def_id: destruct,
164-
substs: cx.tcx.mk_substs_trait(ty, &[ty::ConstnessArg::Required.into()]),
164+
substs: cx.tcx.mk_substs_trait(ty, &[ty::ConstnessArg::Param.into()]),
165165
},
166166
polarity: ty::ImplPolarity::Positive,
167167
}),
@@ -176,7 +176,7 @@ impl Qualif for NeedsNonConstDrop {
176176

177177
if !matches!(
178178
impl_src,
179-
ImplSource::ConstDestruct(_) | ImplSource::Param(_, ty::ConstnessArg::Required)
179+
ImplSource::ConstDestruct(_) | ImplSource::Param(_, ty::ConstnessArg::Required | ty::ConstnessArg::Param)
180180
) {
181181
// If our const destruct candidate is not ConstDestruct or implied by the param env,
182182
// then it's bad

compiler/rustc_infer/src/infer/combine.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,15 @@ impl<'tcx> TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
767767
_ => relate::super_relate_consts(self, c, c),
768768
}
769769
}
770+
771+
fn constness_args(
772+
&mut self,
773+
a: ty::ConstnessArg,
774+
b: ty::ConstnessArg,
775+
) -> RelateResult<'tcx, ty::ConstnessArg> {
776+
assert_eq!(a, b);
777+
Ok(a)
778+
}
770779
}
771780

772781
pub trait ConstEquateRelation<'tcx>: TypeRelation<'tcx> {
@@ -998,4 +1007,12 @@ impl<'tcx> TypeRelation<'tcx> for ConstInferUnifier<'_, 'tcx> {
9981007
_ => relate::super_relate_consts(self, c, c),
9991008
}
10001009
}
1010+
fn constness_args(
1011+
&mut self,
1012+
a: ty::ConstnessArg,
1013+
b: ty::ConstnessArg,
1014+
) -> RelateResult<'tcx, ty::ConstnessArg> {
1015+
assert_eq!(a, b);
1016+
Ok(a)
1017+
}
10011018
}

compiler/rustc_infer/src/infer/equate.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,15 @@ impl<'tcx> TypeRelation<'tcx> for Equate<'_, '_, 'tcx> {
160160
}
161161
Ok(a)
162162
}
163+
164+
fn constness_args(
165+
&mut self,
166+
a: ty::ConstnessArg,
167+
b: ty::ConstnessArg,
168+
) -> RelateResult<'tcx, ty::ConstnessArg> {
169+
// TODO handle infer
170+
relate::super_relate_constness(self, a, b)
171+
}
163172
}
164173

165174
impl<'tcx> ConstEquateRelation<'tcx> for Equate<'_, '_, 'tcx> {

compiler/rustc_infer/src/infer/glb.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use super::Subtype;
77

88
use crate::infer::combine::ConstEquateRelation;
99
use crate::traits::{ObligationCause, PredicateObligation};
10-
use rustc_middle::ty::relate::{Relate, RelateResult, TypeRelation};
10+
use rustc_middle::ty::relate::{self, Relate, RelateResult, TypeRelation};
1111
use rustc_middle::ty::{self, Ty, TyCtxt};
1212

1313
/// "Greatest lower bound" (common subtype)
@@ -110,6 +110,15 @@ impl<'tcx> TypeRelation<'tcx> for Glb<'_, '_, 'tcx> {
110110
Ok(ty::Binder::dummy(self.relate(a.skip_binder(), b.skip_binder())?))
111111
}
112112
}
113+
114+
fn constness_args(
115+
&mut self,
116+
a: ty::ConstnessArg,
117+
b: ty::ConstnessArg,
118+
) -> RelateResult<'tcx, ty::ConstnessArg> {
119+
// TODO.
120+
relate::super_relate_constness(self, a, b)
121+
}
113122
}
114123

115124
impl<'combine, 'infcx, 'tcx> LatticeDir<'infcx, 'tcx> for Glb<'combine, 'infcx, 'tcx> {

compiler/rustc_infer/src/infer/lub.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use super::Subtype;
77

88
use crate::infer::combine::ConstEquateRelation;
99
use crate::traits::{ObligationCause, PredicateObligation};
10-
use rustc_middle::ty::relate::{Relate, RelateResult, TypeRelation};
10+
use rustc_middle::ty::relate::{super_relate_constness, Relate, RelateResult, TypeRelation};
1111
use rustc_middle::ty::{self, Ty, TyCtxt};
1212

1313
/// "Least upper bound" (common supertype)
@@ -110,6 +110,14 @@ impl<'tcx> TypeRelation<'tcx> for Lub<'_, '_, 'tcx> {
110110
Ok(ty::Binder::dummy(self.relate(a.skip_binder(), b.skip_binder())?))
111111
}
112112
}
113+
114+
fn constness_args(
115+
&mut self,
116+
a: ty::ConstnessArg,
117+
b: ty::ConstnessArg,
118+
) -> RelateResult<'tcx, ty::ConstnessArg> {
119+
super_relate_constness(self, a, b)
120+
}
113121
}
114122

115123
impl<'tcx> ConstEquateRelation<'tcx> for Lub<'_, '_, 'tcx> {

compiler/rustc_infer/src/infer/nll_relate/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,15 @@ where
784784

785785
Ok(a)
786786
}
787+
788+
fn constness_args(
789+
&mut self,
790+
a: ty::ConstnessArg,
791+
b: ty::ConstnessArg,
792+
) -> RelateResult<'tcx, ty::ConstnessArg> {
793+
// TODO.
794+
relate::super_relate_constness(self, a, b)
795+
}
787796
}
788797

789798
impl<'tcx, D> ConstEquateRelation<'tcx> for TypeRelating<'_, 'tcx, D>
@@ -1077,4 +1086,13 @@ where
10771086
self.first_free_index.shift_out(1);
10781087
Ok(a.rebind(result))
10791088
}
1089+
1090+
fn constness_args(
1091+
&mut self,
1092+
a: ty::ConstnessArg,
1093+
b: ty::ConstnessArg,
1094+
) -> RelateResult<'tcx, ty::ConstnessArg> {
1095+
// TODO
1096+
relate::super_relate_constness(self, a, b)
1097+
}
10801098
}

compiler/rustc_infer/src/infer/sub.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use super::SubregionOrigin;
44
use crate::infer::combine::ConstEquateRelation;
55
use crate::infer::{TypeVariableOrigin, TypeVariableOriginKind};
66
use crate::traits::Obligation;
7-
use rustc_middle::ty::relate::{Cause, Relate, RelateResult, TypeRelation};
7+
use rustc_middle::ty::relate::{self, Cause, Relate, RelateResult, TypeRelation};
88
use rustc_middle::ty::visit::TypeVisitable;
99
use rustc_middle::ty::TyVar;
1010
use rustc_middle::ty::{self, ToPredicate, Ty, TyCtxt};
@@ -201,6 +201,21 @@ impl<'tcx> TypeRelation<'tcx> for Sub<'_, '_, 'tcx> {
201201
self.fields.higher_ranked_sub(a, b, self.a_is_expected)?;
202202
Ok(a)
203203
}
204+
205+
fn constness_args(
206+
&mut self,
207+
a: ty::ConstnessArg,
208+
b: ty::ConstnessArg,
209+
) -> RelateResult<'tcx, ty::ConstnessArg> {
210+
trace!(?a, ?b);
211+
match (a, b) {
212+
(ty::ConstnessArg::Required, _) => Ok(a),
213+
(a, ty::ConstnessArg::Infer) => Ok(a),
214+
(ty::ConstnessArg::Infer, b) => Ok(b),
215+
(a, ty::ConstnessArg::Not) => Ok(a),
216+
_ => relate::super_relate_constness(self, a, b),
217+
}
218+
}
204219
}
205220

206221
impl<'tcx> ConstEquateRelation<'tcx> for Sub<'_, '_, 'tcx> {

compiler/rustc_middle/src/ty/_match.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,18 @@ impl<'tcx> TypeRelation<'tcx> for Match<'tcx> {
121121
{
122122
Ok(a.rebind(self.relate(a.skip_binder(), b.skip_binder())?))
123123
}
124+
125+
fn constness_args(
126+
&mut self,
127+
a: ty::ConstnessArg,
128+
b: ty::ConstnessArg,
129+
) -> RelateResult<'tcx, ty::ConstnessArg> {
130+
match (a, b) {
131+
(_, ty::ConstnessArg::Infer) => return Ok(a),
132+
(ty::ConstnessArg::Infer, _) => {
133+
return Err(TypeError::ConstnessArgMismatch(relate::expected_found(self, a, b)));
134+
}
135+
_ => relate::super_relate_constness(self, a, b),
136+
}
137+
}
124138
}

compiler/rustc_middle/src/ty/relate.rs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ pub trait TypeRelation<'tcx>: Sized {
9999
) -> RelateResult<'tcx, ty::Binder<'tcx, T>>
100100
where
101101
T: Relate<'tcx>;
102+
103+
fn constness_args(
104+
&mut self,
105+
a: ty::ConstnessArg,
106+
b: ty::ConstnessArg,
107+
) -> RelateResult<'tcx, ty::ConstnessArg>;
102108
}
103109

104110
pub trait Relate<'tcx>: TypeFoldable<'tcx> + Copy {
@@ -141,7 +147,11 @@ pub fn relate_substs<'tcx, R: TypeRelation<'tcx>>(
141147
b_subst: SubstsRef<'tcx>,
142148
) -> RelateResult<'tcx, SubstsRef<'tcx>> {
143149
relation.tcx().mk_substs(iter::zip(a_subst, b_subst).map(|(a, b)| {
144-
relation.relate_with_variance(ty::Invariant, ty::VarianceDiagInfo::default(), a, b)
150+
if let GenericArgKind::Constness(_) = a.unpack() && let GenericArgKind::Constness(_) = b.unpack() {
151+
relation.relate(a, b)
152+
} else {
153+
relation.relate_with_variance(ty::Invariant, ty::VarianceDiagInfo::default(), a, b)
154+
}
145155
}))
146156
}
147157

@@ -232,11 +242,7 @@ impl<'tcx> Relate<'tcx> for ty::ConstnessArg {
232242
a: ty::ConstnessArg,
233243
b: ty::ConstnessArg,
234244
) -> RelateResult<'tcx, ty::ConstnessArg> {
235-
if a == b {
236-
Ok(a)
237-
} else {
238-
Err(TypeError::ConstnessArgMismatch(expected_found(relation, a, b)))
239-
}
245+
relation.constness_args(a, b)
240246
}
241247
}
242248

@@ -660,6 +666,20 @@ pub fn super_relate_consts<'tcx, R: TypeRelation<'tcx>>(
660666
if is_match { Ok(a) } else { Err(TypeError::ConstMismatch(expected_found(relation, a, b))) }
661667
}
662668

669+
pub fn super_relate_constness<'tcx, R: TypeRelation<'tcx>>(
670+
relation: &mut R,
671+
a: ty::ConstnessArg,
672+
b: ty::ConstnessArg,
673+
) -> RelateResult<'tcx, ty::ConstnessArg> {
674+
match (a, b) {
675+
(ty::ConstnessArg::Infer, _) | (_, ty::ConstnessArg::Infer) => {
676+
bug!("var types encountered in super_relate_contness: {a:?} {b:?}");
677+
}
678+
(a, b) if a == b => Ok(a),
679+
_ => Err(TypeError::ConstnessArgMismatch(expected_found(relation, a, b))),
680+
}
681+
}
682+
663683
impl<'tcx> Relate<'tcx> for &'tcx ty::List<ty::Binder<'tcx, ty::ExistentialPredicate<'tcx>>> {
664684
fn relate<R: TypeRelation<'tcx>>(
665685
relation: &mut R,

compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,8 +423,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
423423

424424
// Keep only those bounds which may apply, and propagate overflow if it occurs.
425425
for bound in matching_bounds {
426-
// FIXME(oli-obk): it is suspicious that we are dropping the constness and
427-
// polarity here.
426+
// FIXME(oli-obk): it is suspicious that we are dropping the polarity here.
428427
let wc = self.where_clause_may_apply(stack, bound.map_bound(|t| t.trait_ref))?;
429428
if wc.may_apply() {
430429
candidates.vec.push(ParamCandidate(bound));

compiler/rustc_trait_selection/src/traits/select/confirmation.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,7 +1207,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
12071207
.require_lang_item(LangItem::Destruct, None),
12081208
substs: self.tcx().mk_substs_trait(
12091209
nested_ty,
1210-
&[ty::ConstnessArg::Required.into()],
1210+
&[ty::ConstnessArg::Param.into()],
12111211
),
12121212
},
12131213
polarity: ty::ImplPolarity::Positive,
@@ -1235,7 +1235,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
12351235
def_id: self.tcx().require_lang_item(LangItem::Destruct, None),
12361236
substs: self.tcx().mk_substs_trait(
12371237
nested_ty,
1238-
&[ty::ConstnessArg::Required.into()],
1238+
&[ty::ConstnessArg::Param.into()],
12391239
),
12401240
},
12411241
polarity: ty::ImplPolarity::Positive,

compiler/rustc_trait_selection/src/traits/select/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2232,7 +2232,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
22322232
obligation: &TraitObligation<'tcx>,
22332233
poly_trait_ref: ty::PolyTraitRef<'tcx>,
22342234
) -> Result<Vec<PredicateObligation<'tcx>>, ()> {
2235-
self.infcx
2235+
let ret = self
2236+
.infcx
22362237
.at(&obligation.cause, obligation.param_env)
22372238
// We don't want predicates for opaque types to just match all other types,
22382239
// if there is an obligation on the opaque type, then that obligation must be met
@@ -2241,7 +2242,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
22412242
.define_opaque_types(false)
22422243
.sup(obligation.predicate.to_poly_trait_ref(), poly_trait_ref)
22432244
.map(|InferOk { obligations, .. }| obligations)
2244-
.map_err(|_| ())
2245+
.map_err(|_| ());
2246+
trace!(?ret);
2247+
ret
22452248
}
22462249

22472250
///////////////////////////////////////////////////////////////////////////

compiler/rustc_typeck/src/astconv/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
725725
trait_ref.path.segments.last().unwrap(),
726726
true,
727727
match constness { // TODO check this again
728-
hir::Constness::Const => ty::ConstnessArg::Required,
728+
hir::Constness::Const => ty::ConstnessArg::Param,
729729
hir::Constness::NotConst => ty::ConstnessArg::Not,
730730
},
731731
)

compiler/rustc_typeck/src/check/dropck.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use crate::hir::def_id::{DefId, LocalDefId};
55
use rustc_errors::{struct_span_err, ErrorGuaranteed};
66
use rustc_middle::ty::error::TypeError;
7-
use rustc_middle::ty::relate::{Relate, RelateResult, TypeRelation};
7+
use rustc_middle::ty::relate::{super_relate_constness, Relate, RelateResult, TypeRelation};
88
use rustc_middle::ty::subst::SubstsRef;
99
use rustc_middle::ty::util::IgnoreRegions;
1010
use rustc_middle::ty::{self, Predicate, Ty, TyCtxt};
@@ -323,4 +323,11 @@ impl<'tcx> TypeRelation<'tcx> for SimpleEqRelation<'tcx> {
323323

324324
Ok(a)
325325
}
326+
fn constness_args(
327+
&mut self,
328+
a: ty::ConstnessArg,
329+
b: ty::ConstnessArg,
330+
) -> RelateResult<'tcx, ty::ConstnessArg> {
331+
super_relate_constness(self, a, b)
332+
}
326333
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1465,7 +1465,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14651465
infer_args_for_err: &infer_args_for_err,
14661466
segments,
14671467
},
1468-
None,
1468+
Some(self.constness()),
14691469
)
14701470
});
14711471
assert!(!substs.has_escaping_bound_vars());
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![feature(const_trait_impl)]
2+
3+
#[const_trait]
4+
trait Foo {
5+
fn foo();
6+
}
7+
8+
const fn foo<T: ~const Foo>() {
9+
T::foo();
10+
}
11+
12+
13+
fn main() {}

0 commit comments

Comments
 (0)