Skip to content

Commit

Permalink
Improve error message.
Browse files Browse the repository at this point in the history
  • Loading branch information
zygoloid committed Oct 17, 2022
1 parent 1ed5cba commit e34f7ab
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 4 deletions.
5 changes: 3 additions & 2 deletions explorer/interpreter/impl_scope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,9 @@ auto ImplScope::Resolve(Nonnull<const Value*> constraint_type,
Nonnull<const Value*> current =
type_checker.Substitute(local_bindings, *it);
if (!ValueEqual(first, current, &equality_ctx)) {
return ProgramError(source_loc) << "could not determine that "
<< *first << " == " << *current;
return ProgramError(source_loc)
<< "constraint requires that " << *first
<< " == " << *current << ", which is not known to be true";
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion explorer/testdata/assoc_const/fail_different_type.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ external impl Bad as Iface where .T = Bad {}

fn Main() -> i32 {
F(Good);
// CHECK:STDERR: COMPILATION ERROR: {{.*}}/explorer/testdata/assoc_const/fail_different_type.carbon:[[@LINE+1]]: could not determine that class Bad == i32
// CHECK:STDERR: COMPILATION ERROR: {{.*}}/explorer/testdata/assoc_const/fail_different_type.carbon:[[@LINE+1]]: constraint requires that class Bad == i32, which is not known to be true
F(Bad);
return 0;
}
2 changes: 1 addition & 1 deletion explorer/testdata/assoc_const/fail_different_value.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ external impl Bad as Iface where .N = 4 {}

fn Main() -> i32 {
F(Good);
// CHECK:STDERR: COMPILATION ERROR: {{.*}}/explorer/testdata/assoc_const/fail_different_value.carbon:[[@LINE+1]]: could not determine that 4 == 5
// CHECK:STDERR: COMPILATION ERROR: {{.*}}/explorer/testdata/assoc_const/fail_different_value.carbon:[[@LINE+1]]: constraint requires that 4 == 5, which is not known to be true
F(Bad);
return 0;
}
38 changes: 38 additions & 0 deletions explorer/testdata/assoc_const/fail_equal_indirectly.carbon
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// RUN: %{not} %{explorer} %s | %{FileCheck-strict} %s
// RUN: %{not} %{explorer-trace} %s | %{FileCheck-allow-unmatched} %s
// AUTOUPDATE: %{explorer} %s

package ExplorerTest api;

interface Iface {
let T:! Type;
}

fn F[T:! Iface where .T == i32](x: T) {}

class Class {
impl as Iface where .T = i32 {}
}

// OK, constraint on `F` rewritten to `T:! Iface where U == i32`, which we can
// prove from the constraint on `U`.
fn G[U:! Type where .Self == i32, T:! Iface where .T = U](x: T, y: U) {
F(x);
}

// Not OK: would require looking through two levels of `==`.
fn H[U:! Type where .Self == i32, T:! Iface where .T == U](x: T, y: U) {
// CHECK:STDERR: COMPILATION ERROR: {{.*}}//usr/local/google/home/richardsmith/carbon-lang/explorer/testdata/assoc_const/fail_equal_indirectly.carbon:[[@LINE+1]]: constraint requires that (T).(Iface.T) == i32, which is not known to be true
F(x);
}

fn Main() -> i32 {
var x: Class = {};
G(x, 0);
H(x, 0);
return 0;
}
35 changes: 35 additions & 0 deletions explorer/testdata/assoc_const/fail_equal_to_dependent_type.carbon
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// RUN: %{not} %{explorer} %s | %{FileCheck-strict} %s
// RUN: %{not} %{explorer-trace} %s | %{FileCheck-allow-unmatched} %s
// AUTOUPDATE: %{explorer} %s

package ExplorerTest api;

interface Iface {
let T:! Type;
}

fn F[T:! Iface where .T == i32](x: T) {}

fn G[T:! Iface where .T == i32](x: T) {
F(x);
}

fn H[T:! Iface](x: T) {
// CHECK:STDERR: COMPILATION ERROR: {{.*}}/explorer/testdata/assoc_const/fail_equal_to_dependent_type.carbon:[[@LINE+1]]: constraint requires that (T).(Iface.T) == i32, which is not known to be true
F(x);
}

class Class {
impl as Iface where .T = i32 {}
}

fn Main() -> i32 {
var x: Class = {};
G(x);
H(x);
return 0;
}

0 comments on commit e34f7ab

Please sign in to comment.