Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature interface impl without cast #2628

Closed
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions explorer/interpreter/impl_scope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,29 @@ void ImplScope::Print(llvm::raw_ostream& out) const {
}
}

auto ImplScope::GetInterfacesOfType(Nonnull<const Value*> type) const
-> std::vector<Nonnull<const InterfaceType*>> {
std::vector<Nonnull<const InterfaceType*>> result;
if (type->kind() == Value::Kind::NominalClassType) {
for (const Impl& impl : impls_) {
if (impl.type->kind() == Value::Kind::NominalClassType) {
const auto& p1 = cast<NominalClassType>(*type);
const auto& p2 = cast<NominalClassType>(*impl.type);
if (&p1.declaration() == &p2.declaration()) {
result.push_back(impl.interface);
}
}
}
for (const Nonnull<const ImplScope*>& parent : parent_scopes_) {
auto part = parent->GetInterfacesOfType(type);
for (const auto& x : part) {
result.push_back(x);
}
}
}
return result;
}

auto SingleStepEqualityContext::VisitEqualValues(
Nonnull<const Value*> value,
llvm::function_ref<bool(Nonnull<const Value*>)> visitor) const -> bool {
Expand Down
3 changes: 3 additions & 0 deletions explorer/interpreter/impl_scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ class ImplScope {

void Print(llvm::raw_ostream& out) const;

auto GetInterfacesOfType(Nonnull<const Value*> type) const
-> std::vector<Nonnull<const InterfaceType*>>;

// The `Impl` struct is a key-value pair where the key is the
// combination of a type and an interface, e.g., `List` and `Container`,
// and the value is the result of statically resolving to the `impl`
Expand Down
36 changes: 33 additions & 3 deletions explorer/interpreter/type_checker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2671,9 +2671,39 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e,
}
return Success();
} else {
return ProgramError(e->source_loc())
<< "class " << t_class.declaration().name()
<< " does not have a field named " << access.member_name();
auto ifaces = impl_scope.GetInterfacesOfType(&object_type);
if (ifaces.size() > 0) {
pmqtt marked this conversation as resolved.
Show resolved Hide resolved
CARBON_ASSIGN_OR_RETURN(
ConstraintLookupResult result,
LookupInConstraint(e->source_loc(), "member access",
ifaces[0], access.member_name()));

CARBON_ASSIGN_OR_RETURN(
Nonnull<const Witness*> witness,
impl_scope.Resolve(ifaces[0], &object_type, e->source_loc(),
*this));

CARBON_ASSIGN_OR_RETURN(
Nonnull<const Witness*> impl,
impl_scope.Resolve(ifaces[0], &object_type, e->source_loc(),
*this));
access.set_member(arena_->New<NamedElement>(result.member));
access.set_impl(impl);
access.set_found_in_interface(ifaces[0]);
const Value& member_type = result.member->static_type();
Bindings bindings = result.interface->bindings();
bindings.Add(result.interface->declaration().self(), &object_type,
witness);
Nonnull<const Value*> inst_member_type =
Substitute(bindings, &member_type);
access.set_static_type(inst_member_type);
access.set_value_category(ValueCategory::Let);
return Success();
} else {
return ProgramError(e->source_loc())
<< "class " << t_class.declaration().name()
<< " does not have a field named " << access.member_name();
}
}
}
case Value::Kind::VariableType:
Expand Down
42 changes: 42 additions & 0 deletions explorer/testdata/interface/find_interface_impl_for_class.carbon
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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: (
// CHECK:STDOUT: test
// CHECK:STDOUT: )
// CHECK:STDOUT: result: 42

package ExplorerTest api;

interface Printable {
fn PrintIt[self: Self]();
}

impl String as Printable {
fn PrintIt[self: String]() {
Print(self);
}
}

class Vector(T:! type) {
var x: T;
}

// Conditionally implement the API for certain `T`s.
impl forall [U:! Printable] Vector(U) as Printable {
fn PrintIt[self: Self]() {
Print("(");
self.x.PrintIt();
Print(")");
}
}

fn Main() -> i32 {
var v: Vector(String) = {.x = "test"};
v.PrintIt();
return 42;
}