Files
carbon-lang/explorer/testdata/class/non_virtual_dispatch_abstract.carbon
T
Adrien Leravat 026c4b9dc3 Explorer: move subtyping logic to TypeChecker (#2484)
Addresses comments from this discussion: https://github.com/carbon-language/carbon-lang/pull/2460#discussion_r1046444433

Features:
* Move subtyping logic from Interpreter to TypeChecker, exposing subtyping as a series of access to `.base`.
    * Excludes function parameter conversion, which is still done in ::Convert due to parameters conversion being handled differently.

Changes:
* Add new `class BaseAccessExpression : public MemberAccessExpression`, allowing rewrites
* Handle `BaseAccessExpression` expression type in Interpreter
* Move subtyping logic to `TypeChecker::ImplicitlyConvert`
2022-12-21 19:01:02 -08:00

43 lines
862 B
Plaintext

// 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: (*c).a: 1
// CHECK:STDOUT: (*c).Foo(): 1
// CHECK:STDOUT: (*c).Bar(): 1
// CHECK:STDOUT: result: 0
package ExplorerTest api;
abstract class C {
var a: i32;
fn Foo[self: Self]() -> i32 {
return 1;
}
fn Bar() -> i32 {
return 1;
}
}
class D extends C {
var b: i32;
fn Foo[self: Self]() -> i32 {
return 2;
}
fn Bar() -> i32 {
return 2;
}
}
fn Main() -> i32 {
var d: D = { .base = {.a = 1}, .b = 2 };
var c: C* = &d;
Print("(*c).a: {0}", (*c).a);
Print("(*c).Foo(): {0}", (*c).Foo());
Print("(*c).Bar(): {0}", (*c).Bar());
return 0;
}