Skip to content

Commit 3e8acaf

Browse files
authored
Rollup merge of #65579 - skinny121:resolve_const_vars, r=varkor
Changed `resolve_type_vars_with_obligations` to also resolve const inference variables Fixes #65380 r? @varkor
2 parents cc42adf + 9cefcd3 commit 3e8acaf

File tree

10 files changed

+37
-34
lines changed

10 files changed

+37
-34
lines changed

src/librustc/infer/resolve.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::{InferCtxt, FixupError, FixupResult, Span};
22
use super::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
33
use crate::mir::interpret::ConstValue;
4-
use crate::ty::{self, Ty, Const, TyCtxt, TypeFoldable, InferConst, TypeFlags};
4+
use crate::ty::{self, Ty, Const, TyCtxt, TypeFoldable, InferConst};
55
use crate::ty::fold::{TypeFolder, TypeVisitor};
66

77
///////////////////////////////////////////////////////////////////////////
@@ -29,7 +29,7 @@ impl<'a, 'tcx> TypeFolder<'tcx> for OpportunisticVarResolver<'a, 'tcx> {
2929
}
3030

3131
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
32-
if !t.has_infer_types() {
32+
if !t.has_infer_types() && !t.has_infer_consts() {
3333
t // micro-optimize -- if there is nothing in this type that this fold affects...
3434
} else {
3535
let t = self.infcx.shallow_resolve(t);
@@ -38,7 +38,7 @@ impl<'a, 'tcx> TypeFolder<'tcx> for OpportunisticVarResolver<'a, 'tcx> {
3838
}
3939

4040
fn fold_const(&mut self, ct: &'tcx Const<'tcx>) -> &'tcx Const<'tcx> {
41-
if !ct.has_type_flags(TypeFlags::HAS_CT_INFER) {
41+
if !ct.has_infer_consts() {
4242
ct // micro-optimize -- if there is nothing in this const that this fold affects...
4343
} else {
4444
let ct = self.infcx.shallow_resolve(ct);

src/librustc/ty/fold.rs

+3
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ pub trait TypeFoldable<'tcx>: fmt::Debug + Clone {
8888
fn has_infer_types(&self) -> bool {
8989
self.has_type_flags(TypeFlags::HAS_TY_INFER)
9090
}
91+
fn has_infer_consts(&self) -> bool {
92+
self.has_type_flags(TypeFlags::HAS_CT_INFER)
93+
}
9194
fn has_local_value(&self) -> bool {
9295
self.has_type_flags(TypeFlags::KEEP_IN_LOCAL_TCX)
9396
}

src/librustc_typeck/check/coercion.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
811811
target: Ty<'tcx>,
812812
allow_two_phase: AllowTwoPhase,
813813
) -> RelateResult<'tcx, Ty<'tcx>> {
814-
let source = self.resolve_type_vars_with_obligations(expr_ty);
814+
let source = self.resolve_vars_with_obligations(expr_ty);
815815
debug!("coercion::try({:?}: {:?} -> {:?})", expr, source, target);
816816

817817
let cause = self.cause(expr.span, ObligationCauseCode::ExprAssignable);
@@ -829,7 +829,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
829829

830830
/// Same as `try_coerce()`, but without side-effects.
831831
pub fn can_coerce(&self, expr_ty: Ty<'tcx>, target: Ty<'tcx>) -> bool {
832-
let source = self.resolve_type_vars_with_obligations(expr_ty);
832+
let source = self.resolve_vars_with_obligations(expr_ty);
833833
debug!("coercion::can({:?} -> {:?})", source, target);
834834

835835
let cause = self.cause(syntax_pos::DUMMY_SP, ObligationCauseCode::ExprAssignable);
@@ -853,8 +853,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
853853
-> RelateResult<'tcx, Ty<'tcx>>
854854
where E: AsCoercionSite
855855
{
856-
let prev_ty = self.resolve_type_vars_with_obligations(prev_ty);
857-
let new_ty = self.resolve_type_vars_with_obligations(new_ty);
856+
let prev_ty = self.resolve_vars_with_obligations(prev_ty);
857+
let new_ty = self.resolve_vars_with_obligations(new_ty);
858858
debug!("coercion::try_find_coercion_lub({:?}, {:?})", prev_ty, new_ty);
859859

860860
// Special-case that coercion alone cannot handle:
@@ -1333,7 +1333,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
13331333
err.span_label(return_sp, "expected because this return type...");
13341334
err.span_label( *sp, format!(
13351335
"...is found to be `{}` here",
1336-
fcx.resolve_type_vars_with_obligations(expected),
1336+
fcx.resolve_vars_with_obligations(expected),
13371337
));
13381338
}
13391339
err

src/librustc_typeck/check/demand.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
108108
expected: Ty<'tcx>,
109109
allow_two_phase: AllowTwoPhase)
110110
-> (Ty<'tcx>, Option<DiagnosticBuilder<'tcx>>) {
111-
let expected = self.resolve_type_vars_with_obligations(expected);
111+
let expected = self.resolve_vars_with_obligations(expected);
112112

113113
let e = match self.try_coerce(expr, checked_ty, expected, allow_two_phase) {
114114
Ok(ty) => return (ty, None),
@@ -117,7 +117,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
117117

118118
let expr = expr.peel_drop_temps();
119119
let cause = self.misc(expr.span);
120-
let expr_ty = self.resolve_type_vars_with_obligations(checked_ty);
120+
let expr_ty = self.resolve_vars_with_obligations(checked_ty);
121121
let mut err = self.report_mismatched_types(&cause, expected, expr_ty, e);
122122

123123
if self.is_assign_to_bool(expr, expected) {

src/librustc_typeck/check/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1010,7 +1010,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10101010
expr: &'tcx hir::Expr,
10111011
) -> Ty<'tcx> {
10121012
let flds = expected.only_has_type(self).and_then(|ty| {
1013-
let ty = self.resolve_type_vars_with_obligations(ty);
1013+
let ty = self.resolve_vars_with_obligations(ty);
10141014
match ty.kind {
10151015
ty::Tuple(ref flds) => Some(&flds[..]),
10161016
_ => None

src/librustc_typeck/check/method/suggest.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
919919
// This occurs for UFCS desugaring of `T::method`, where there is no
920920
// receiver expression for the method call, and thus no autoderef.
921921
if let SelfSource::QPath(_) = source {
922-
return is_local(self.resolve_type_vars_with_obligations(rcvr_ty));
922+
return is_local(self.resolve_vars_with_obligations(rcvr_ty));
923923
}
924924

925925
self.autoderef(span, rcvr_ty).any(|(ty, _)| is_local(ty))

src/librustc_typeck/check/mod.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -2440,23 +2440,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
24402440
self.cause(span, ObligationCauseCode::MiscObligation)
24412441
}
24422442

2443-
/// Resolves type variables in `ty` if possible. Unlike the infcx
2443+
/// Resolves type and const variables in `ty` if possible. Unlike the infcx
24442444
/// version (resolve_vars_if_possible), this version will
24452445
/// also select obligations if it seems useful, in an effort
24462446
/// to get more type information.
2447-
fn resolve_type_vars_with_obligations(&self, mut ty: Ty<'tcx>) -> Ty<'tcx> {
2448-
debug!("resolve_type_vars_with_obligations(ty={:?})", ty);
2447+
fn resolve_vars_with_obligations(&self, mut ty: Ty<'tcx>) -> Ty<'tcx> {
2448+
debug!("resolve_vars_with_obligations(ty={:?})", ty);
24492449

24502450
// No Infer()? Nothing needs doing.
2451-
if !ty.has_infer_types() {
2452-
debug!("resolve_type_vars_with_obligations: ty={:?}", ty);
2451+
if !ty.has_infer_types() && !ty.has_infer_consts() {
2452+
debug!("resolve_vars_with_obligations: ty={:?}", ty);
24532453
return ty;
24542454
}
24552455

24562456
// If `ty` is a type variable, see whether we already know what it is.
24572457
ty = self.resolve_vars_if_possible(&ty);
2458-
if !ty.has_infer_types() {
2459-
debug!("resolve_type_vars_with_obligations: ty={:?}", ty);
2458+
if !ty.has_infer_types() && !ty.has_infer_consts() {
2459+
debug!("resolve_vars_with_obligations: ty={:?}", ty);
24602460
return ty;
24612461
}
24622462

@@ -2467,7 +2467,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
24672467
self.select_obligations_where_possible(false, |_| {});
24682468
ty = self.resolve_vars_if_possible(&ty);
24692469

2470-
debug!("resolve_type_vars_with_obligations: ty={:?}", ty);
2470+
debug!("resolve_vars_with_obligations: ty={:?}", ty);
24712471
ty
24722472
}
24732473

@@ -3668,7 +3668,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
36683668
formal_ret: Ty<'tcx>,
36693669
formal_args: &[Ty<'tcx>])
36703670
-> Vec<Ty<'tcx>> {
3671-
let formal_ret = self.resolve_type_vars_with_obligations(formal_ret);
3671+
let formal_ret = self.resolve_vars_with_obligations(formal_ret);
36723672
let ret_ty = match expected_ret.only_has_type(self) {
36733673
Some(ret) => ret,
36743674
None => return Vec::new()
@@ -4517,7 +4517,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
45174517
err.span_suggestion(
45184518
span,
45194519
"try adding a return type",
4520-
format!("-> {} ", self.resolve_type_vars_with_obligations(found)),
4520+
format!("-> {} ", self.resolve_vars_with_obligations(found)),
45214521
Applicability::MachineApplicable);
45224522
true
45234523
}
@@ -4993,7 +4993,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
49934993
// If no resolution is possible, then an error is reported.
49944994
// Numeric inference variables may be left unresolved.
49954995
pub fn structurally_resolved_type(&self, sp: Span, ty: Ty<'tcx>) -> Ty<'tcx> {
4996-
let ty = self.resolve_type_vars_with_obligations(ty);
4996+
let ty = self.resolve_vars_with_obligations(ty);
49974997
if !ty.is_ty_var() {
49984998
ty
49994999
} else {

src/librustc_typeck/check/op.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
179179
self.check_expr_with_needs(lhs_expr, Needs::MutPlace)
180180
}
181181
};
182-
let lhs_ty = self.resolve_type_vars_with_obligations(lhs_ty);
182+
let lhs_ty = self.resolve_vars_with_obligations(lhs_ty);
183183

184184
// N.B., as we have not yet type-checked the RHS, we don't have the
185185
// type at hand. Make a variable to represent it. The whole reason
@@ -196,7 +196,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
196196

197197
// see `NB` above
198198
let rhs_ty = self.check_expr_coercable_to_type(rhs_expr, rhs_ty_var);
199-
let rhs_ty = self.resolve_type_vars_with_obligations(rhs_ty);
199+
let rhs_ty = self.resolve_vars_with_obligations(rhs_ty);
200200

201201
let return_ty = match result {
202202
Ok(method) => {

src/librustc_typeck/check/pat.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
251251
expected: Ty<'tcx>,
252252
mut def_bm: BindingMode,
253253
) -> (Ty<'tcx>, BindingMode) {
254-
let mut expected = self.resolve_type_vars_with_obligations(&expected);
254+
let mut expected = self.resolve_vars_with_obligations(&expected);
255255

256256
// Peel off as many `&` or `&mut` from the scrutinee type as possible. For example,
257257
// for `match &&&mut Some(5)` the loop runs three times, aborting when it reaches

src/test/ui/const-generics/const-argument-cross-crate-mismatch.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
error[E0308]: mismatched types
2-
--> $DIR/const-argument-cross-crate-mismatch.rs:6:41
2+
--> $DIR/const-argument-cross-crate-mismatch.rs:6:67
33
|
44
LL | let _ = const_generic_lib::function(const_generic_lib::Struct([0u8, 1u8]));
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `3usize`, found `2usize`
5+
| ^^^^^^^^^^ expected an array with a fixed size of 3 elements, found one with 2 elements
66
|
7-
= note: expected type `const_generic_lib::Struct<3usize>`
8-
found type `const_generic_lib::Struct<_: usize>`
7+
= note: expected type `[u8; 3]`
8+
found type `[u8; 2]`
99

1010
error[E0308]: mismatched types
11-
--> $DIR/const-argument-cross-crate-mismatch.rs:8:39
11+
--> $DIR/const-argument-cross-crate-mismatch.rs:8:65
1212
|
1313
LL | let _: const_generic_lib::Alias = const_generic_lib::Struct([0u8, 1u8, 2u8]);
14-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `2usize`, found `3usize`
14+
| ^^^^^^^^^^^^^^^ expected an array with a fixed size of 2 elements, found one with 3 elements
1515
|
16-
= note: expected type `const_generic_lib::Struct<2usize>`
17-
found type `const_generic_lib::Struct<_: usize>`
16+
= note: expected type `[u8; 2]`
17+
found type `[u8; 3]`
1818

1919
error: aborting due to 2 previous errors
2020

0 commit comments

Comments
 (0)