Skip to content

Commit 563aeb1

Browse files
committed
[Sema] be more descriptive when hardening fails
1 parent 301fab2 commit 563aeb1

3 files changed

Lines changed: 36 additions & 3 deletions

File tree

src/sema.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,12 +1085,16 @@ Sema::hardenTypeIfNeeded(res::Context &ctx, res::Expr *expr, res::Type *to) {
10851085
if (!fromInner || !toInner)
10861086
return expr;
10871087

1088-
if (!ctx.canUnify(fromInner, toInner))
1088+
if (!toInner->getAs<res::UninferredType>() &&
1089+
!ctx.canUnify(fromInner, toInner))
10891090
return expr;
10901091

1091-
ctx.unify(fromInner, toInner);
10921092
auto *harden = res::ImplicitHardening::create(ctx, expr->location, expr);
1093-
harden->setType(to);
1093+
if (to->getAs<res::PointerType>())
1094+
harden->setType(res::PointerType::create(ctx, fromInner, false));
1095+
else
1096+
harden->setType(res::RefType::create(ctx, fromInner, false));
1097+
10941098
return harden;
10951099
}
10961100

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: compiler %s -res-dump 2>&1 | filecheck %s
2+
trait A {
3+
fn foo(self: &Self);
4+
}
5+
6+
fn foo<T : A>(t: &T) {
7+
t.foo();
8+
}
9+
10+
fn bar(mn: &mut number) {
11+
// CHECK: [[# @LINE + 1 ]]:7: error: cannot satisfy requirement 'number : A'
12+
foo(mn);
13+
}
14+
15+
fn main() {}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: compiler %s -res-dump 2>&1 | filecheck %s
2+
trait A {
3+
fn foo(self: &Self);
4+
}
5+
6+
fn foo<T : A>(t: *T) {
7+
t.foo();
8+
}
9+
10+
fn main() {
11+
let p = gc mut 123;
12+
// CHECK: [[# @LINE + 1 ]]:7: error: cannot satisfy requirement 'number : A'
13+
foo(p);
14+
}

0 commit comments

Comments
 (0)