Remove ExpectType and some calls to IsImplicitlyConvertible. (#2647)

These functions are dangerous, as they check whether conversions are possible without actually performing the conversions. In each case where they were used, explorer would crash in some cases if a user-defined conversion is required.

This change moves us more towards implicit conversions being handled by a regular function call on an interface and away from them being magical builtins. Unfortunately, this exposes a pre-existing bug that a call of the form `x.(ImplicitAs(T).Convert)()` compiles even if `x` only has an explicit conversion to `T`. That's worked around here for now, but will need a proper fix later.
This commit is contained in:
Richard Smith
2023-03-02 14:04:44 -08:00
committed by GitHub
parent 21c3f64564
commit c8141b59d9
16 changed files with 152 additions and 53 deletions
@@ -13,7 +13,7 @@ class A {}
fn Main() -> i32 {
var a: A = {};
var b: A* = &a;
// CHECK:STDERR: COMPILATION ERROR: {{.*}}/explorer/testdata/pointer/fail_invalid_ptr_conversion1.carbon:[[@LINE+1]]: type error in name binding: 'class A*' is not implicitly convertible to 'i32'
// CHECK:STDERR: COMPILATION ERROR: {{.*}}/explorer/testdata/pointer/fail_invalid_ptr_conversion1.carbon:[[@LINE+1]]: type error in initializer of variable: 'class A*' is not implicitly convertible to 'i32'
var c: i32 = b;
return 1;
}
@@ -13,7 +13,7 @@ class A {}
fn Main() -> i32 {
var a: i32 = 0;
var b: i32* = &a;
// CHECK:STDERR: COMPILATION ERROR: {{.*}}/explorer/testdata/pointer/fail_invalid_ptr_conversion2.carbon:[[@LINE+1]]: type error in name binding: 'i32*' is not implicitly convertible to 'class A*'
// CHECK:STDERR: COMPILATION ERROR: {{.*}}/explorer/testdata/pointer/fail_invalid_ptr_conversion2.carbon:[[@LINE+1]]: type error in initializer of variable: 'i32*' is not implicitly convertible to 'class A*'
var c: A* = b;
return 1;
}
+25
View File
@@ -0,0 +1,25 @@
// 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
//
// AUTOUPDATE
// RUN: %{explorer-run}
// RUN: %{explorer-run-trace}
// CHECK:STDOUT: result: 1
package ExplorerTest api;
class TypeLike {
var v: type;
impl as ImplicitAs(type) {
fn Convert[self: Self]() -> type { return i32; }
}
}
fn Almosti32() -> TypeLike { return {.v = i32}; }
fn Main() -> i32 {
var a: Almosti32() = 1;
var p: Almosti32()* = &a;
return *p;
}
+25
View File
@@ -0,0 +1,25 @@
// 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
//
// AUTOUPDATE
// RUN: %{explorer-run}
// RUN: %{explorer-run-trace}
// CHECK:STDOUT: 3
// CHECK:STDOUT: 4
// CHECK:STDOUT: result: 0
package ExplorerTest api;
fn Main() -> i32 {
var a: (i32, i32) = (1, 2);
var p: (i32, i32)* = &a;
a[0] = 3;
Print("{0}", (*p)[0]);
(*p)[1] = 4;
Print("{0}", a[1]);
return 0;
}