Files
carbon-lang/explorer/testdata/template/instantiate_from_generic.carbon
T
Richard SmithandGeoff Romer 4e1adcf4c7 Bring constraints into scope when a generic calls a template. (#2878)
When a template has an argument that involves a generic parameter, we're supposed to delay instantiation until we know the concrete value, but explorer is not set up to do that yet, so for now we instead instantiate the template with the symbolic argument. When that happens, bring the constraints on the generic parameter into scope so they can be used inside the template instantiation.

This requires adding a new search over a value for the generic parameters that appear within it; a `VisitNestedValues` visitor is added to visit all the `Value`s nested with a value, and also convert an existing place where we were doing the same thing in a way that was incorrect (but harmlessly incorrect for now) to use it.

This is needed by #2881, which needs implementations of `ImplicitAs` for nested types when instantiating a builtin impl of `ImplicitAs` for an aggregate type.

Co-authored-by: Geoff Romer <gromer@google.com>
2023-06-09 10:09:52 -07:00

35 lines
839 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
// CHECK:STDOUT: 1
// CHECK:STDOUT: result: 0
package ExplorerTest api;
interface I { fn F[self: Self](); }
fn CheckTimplsI[T:! I](x: T) { x.F(); }
interface J { fn F[self: Self](); }
impl forall [template T:! I] T as J {
fn F[self: Self]() { CheckTimplsI(self); }
}
// Ensure that the instantiated `impl T as J` is type-checked in an impl scope
// where `T impls I` is available, so that its call to `CheckTimplsI` is valid.
fn UseTemplatedImplFromGeneric[T:! I](x: T) { x.(J.F)(); }
impl i32 as I {
fn F[self: i32]() {
Print("{0}", self);
}
}
fn Main() -> i32 {
UseTemplatedImplFromGeneric(1);
return 0;
}