Skip to content

Commit 78cf8cc

Browse files
authoredDec 15, 2022
Rollup merge of #105633 - compiler-errors:term-agnostic, r=oli-obk
Make `report_projection_error` more `Term` agnostic Fixes #105632
2 parents a2c9f2a + 7bf36de commit 78cf8cc

File tree

5 files changed

+91
-20
lines changed

5 files changed

+91
-20
lines changed
 

‎compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+26-18
Original file line numberDiff line numberDiff line change
@@ -1636,17 +1636,30 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
16361636
infer::LateBoundRegionConversionTime::HigherRankedType,
16371637
bound_predicate.rebind(data),
16381638
);
1639-
let normalized_ty = ocx.normalize(
1640-
&obligation.cause,
1641-
obligation.param_env,
1642-
self.tcx.mk_projection(data.projection_ty.def_id, data.projection_ty.substs),
1643-
);
1639+
let unnormalized_term = match data.term.unpack() {
1640+
ty::TermKind::Ty(_) => self
1641+
.tcx
1642+
.mk_projection(data.projection_ty.def_id, data.projection_ty.substs)
1643+
.into(),
1644+
ty::TermKind::Const(ct) => self
1645+
.tcx
1646+
.mk_const(
1647+
ty::UnevaluatedConst {
1648+
def: ty::WithOptConstParam::unknown(data.projection_ty.def_id),
1649+
substs: data.projection_ty.substs,
1650+
},
1651+
ct.ty(),
1652+
)
1653+
.into(),
1654+
};
1655+
let normalized_term =
1656+
ocx.normalize(&obligation.cause, obligation.param_env, unnormalized_term);
16441657

16451658
debug!(?obligation.cause, ?obligation.param_env);
16461659

1647-
debug!(?normalized_ty, data.ty = ?data.term);
1660+
debug!(?normalized_term, data.ty = ?data.term);
16481661

1649-
let is_normalized_ty_expected = !matches!(
1662+
let is_normalized_term_expected = !matches!(
16501663
obligation.cause.code().peel_derives(),
16511664
ObligationCauseCode::ItemObligation(_)
16521665
| ObligationCauseCode::BindingObligation(_, _)
@@ -1655,7 +1668,6 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
16551668
| ObligationCauseCode::ObjectCastObligation(..)
16561669
| ObligationCauseCode::OpaqueType
16571670
);
1658-
let expected_ty = data.term.ty().unwrap_or_else(|| self.tcx.ty_error());
16591671

16601672
// constrain inference variables a bit more to nested obligations from normalize so
16611673
// we can have more helpful errors.
@@ -1664,11 +1676,11 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
16641676
if let Err(new_err) = ocx.eq_exp(
16651677
&obligation.cause,
16661678
obligation.param_env,
1667-
is_normalized_ty_expected,
1668-
normalized_ty,
1669-
expected_ty,
1679+
is_normalized_term_expected,
1680+
normalized_term,
1681+
data.term,
16701682
) {
1671-
(Some((data, is_normalized_ty_expected, normalized_ty, expected_ty)), new_err)
1683+
(Some((data, is_normalized_term_expected, normalized_term, data.term)), new_err)
16721684
} else {
16731685
(None, error.err)
16741686
}
@@ -1677,12 +1689,8 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
16771689
};
16781690

16791691
let msg = values
1680-
.and_then(|(predicate, _, normalized_ty, expected_ty)| {
1681-
self.maybe_detailed_projection_msg(
1682-
predicate,
1683-
normalized_ty.into(),
1684-
expected_ty.into(),
1685-
)
1692+
.and_then(|(predicate, _, normalized_term, expected_term)| {
1693+
self.maybe_detailed_projection_msg(predicate, normalized_term, expected_term)
16861694
})
16871695
.unwrap_or_else(|| format!("type mismatch resolving `{}`", predicate));
16881696
let mut diag = struct_span_err!(self.tcx.sess, obligation.cause.span, E0271, "{msg}");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
warning: the feature `generic_const_exprs` is incomplete and may not be safe to use and/or cause compiler crashes
2+
--> $DIR/const-projection-err.rs:4:26
3+
|
4+
LL | #![cfg_attr(gce, feature(generic_const_exprs))]
5+
| ^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #76560 <https://github.com/rust-lang/rust/issues/76560> for more information
8+
= note: `#[warn(incomplete_features)]` on by default
9+
10+
error[E0271]: type mismatch resolving `<T as TraitWAssocConst>::A == 1`
11+
--> $DIR/const-projection-err.rs:14:11
12+
|
13+
LL | foo::<T>();
14+
| ^ expected `0`, found `1`
15+
|
16+
note: required by a bound in `foo`
17+
--> $DIR/const-projection-err.rs:11:28
18+
|
19+
LL | fn foo<T: TraitWAssocConst<A = 1>>() {}
20+
| ^^^^^ required by this bound in `foo`
21+
22+
error: aborting due to previous error; 1 warning emitted
23+
24+
For more information about this error, try `rustc --explain E0271`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// revisions: stock gce
2+
3+
#![feature(associated_const_equality)]
4+
#![cfg_attr(gce, feature(generic_const_exprs))]
5+
//[gce]~^ WARN the feature `generic_const_exprs` is incomplete
6+
7+
trait TraitWAssocConst {
8+
const A: usize;
9+
}
10+
11+
fn foo<T: TraitWAssocConst<A = 1>>() {}
12+
13+
fn bar<T: TraitWAssocConst<A = 0>>() {
14+
foo::<T>();
15+
//~^ ERROR type mismatch resolving `<T as TraitWAssocConst>::A == 1`
16+
}
17+
18+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0271]: type mismatch resolving `<T as TraitWAssocConst>::A == 1`
2+
--> $DIR/const-projection-err.rs:14:11
3+
|
4+
LL | foo::<T>();
5+
| ^ expected `1`, found `<T as TraitWAssocConst>::A`
6+
|
7+
= note: expected constant `1`
8+
found constant `<T as TraitWAssocConst>::A`
9+
note: required by a bound in `foo`
10+
--> $DIR/const-projection-err.rs:11:28
11+
|
12+
LL | fn foo<T: TraitWAssocConst<A = 1>>() {}
13+
| ^^^^^ required by this bound in `foo`
14+
15+
error: aborting due to previous error
16+
17+
For more information about this error, try `rustc --explain E0271`.

‎src/test/ui/issues/issue-105330.stderr

+6-2
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@ error[E0271]: type mismatch resolving `<Demo as TraitWAssocConst>::A == 32`
5555
--> $DIR/issue-105330.rs:12:11
5656
|
5757
LL | foo::<Demo>()();
58-
| ^^^^ types differ
58+
| ^^^^ expected `32`, found `<Demo as TraitWAssocConst>::A`
5959
|
60+
= note: expected constant `32`
61+
found constant `<Demo as TraitWAssocConst>::A`
6062
note: required by a bound in `foo`
6163
--> $DIR/issue-105330.rs:11:28
6264
|
@@ -89,8 +91,10 @@ error[E0271]: type mismatch resolving `<Demo as TraitWAssocConst>::A == 32`
8991
--> $DIR/issue-105330.rs:19:11
9092
|
9193
LL | foo::<Demo>();
92-
| ^^^^ types differ
94+
| ^^^^ expected `32`, found `<Demo as TraitWAssocConst>::A`
9395
|
96+
= note: expected constant `32`
97+
found constant `<Demo as TraitWAssocConst>::A`
9498
note: required by a bound in `foo`
9599
--> $DIR/issue-105330.rs:11:28
96100
|

0 commit comments

Comments
 (0)
Please sign in to comment.