Skip to content

Commit

Permalink
Rollup merge of #78249 - lcnr:ct-infer-origin, r=varkor
Browse files Browse the repository at this point in the history
improve const infer error

For type inference we probably have to be careful about subtyping and stuff but considering that subtyping shouldn't be relevant for constants I don't really see a reason why we may not want to reuse the const origin here.

r? `@varkor`
  • Loading branch information
jonas-schievink committed Oct 24, 2020
2 parents 6b2ed99 + e1c524c commit 77cd5b5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 18 deletions.
25 changes: 8 additions & 17 deletions compiler/rustc_middle/src/infer/unify_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,19 +175,15 @@ impl<'tcx> UnifyKey for ty::ConstVid<'tcx> {
impl<'tcx> UnifyValue for ConstVarValue<'tcx> {
type Error = (&'tcx ty::Const<'tcx>, &'tcx ty::Const<'tcx>);

fn unify_values(value1: &Self, value2: &Self) -> Result<Self, Self::Error> {
let (val, span) = match (value1.val, value2.val) {
fn unify_values(&value1: &Self, &value2: &Self) -> Result<Self, Self::Error> {
Ok(match (value1.val, value2.val) {
(ConstVariableValue::Known { .. }, ConstVariableValue::Known { .. }) => {
bug!("equating two const variables, both of which have known values")
}

// If one side is known, prefer that one.
(ConstVariableValue::Known { .. }, ConstVariableValue::Unknown { .. }) => {
(value1.val, value1.origin.span)
}
(ConstVariableValue::Unknown { .. }, ConstVariableValue::Known { .. }) => {
(value2.val, value2.origin.span)
}
(ConstVariableValue::Known { .. }, ConstVariableValue::Unknown { .. }) => value1,
(ConstVariableValue::Unknown { .. }, ConstVariableValue::Known { .. }) => value2,

// If both sides are *unknown*, it hardly matters, does it?
(
Expand All @@ -200,16 +196,11 @@ impl<'tcx> UnifyValue for ConstVarValue<'tcx> {
// universe is the minimum of the two universes, because that is
// the one which contains the fewest names in scope.
let universe = cmp::min(universe1, universe2);
(ConstVariableValue::Unknown { universe }, value1.origin.span)
ConstVarValue {
val: ConstVariableValue::Unknown { universe },
origin: value1.origin,
}
}
};

Ok(ConstVarValue {
origin: ConstVariableOrigin {
kind: ConstVariableOriginKind::ConstInference,
span: span,
},
val,
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/const-generics/infer/issue-77092.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0282]: type annotations needed
--> $DIR/issue-77092.rs:13:26
|
LL | println!("{:?}", take_array_from_mut(&mut arr, i));
| ^^^^^^^^^^^^^^^^^^^ cannot infer the value of the constant `{_: usize}`
| ^^^^^^^^^^^^^^^^^^^ cannot infer the value of const parameter `N` declared on the function `take_array_from_mut`

error: aborting due to previous error

Expand Down

0 comments on commit 77cd5b5

Please sign in to comment.