mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-28 10:00:10 +01:00
The strategy that we use for now to support template instantiation is to check the impl declaration as if it were a generic, but to defer all checking of the impl definition until we see a use in which all template parameters have arguments. At that point, we clone the impl definition and type-check the whole thing, with constant values set on the template parameters corresponding to the given arguments. No caching of template instantiations is performed yet; each time we form a reference to a template instantiation, we instantiate it afresh. We also don't implement the name lookup rule from #949 yet; lookups during template instantiation look only in the actual type and not in the constraint. Depends on #2699
33 lines
688 B
Plaintext
33 lines
688 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: 1
|
|
// CHECK:STDOUT: 3
|
|
// CHECK:STDOUT: result: 0
|
|
|
|
package ExplorerTest api;
|
|
|
|
interface GetX {
|
|
fn DoIt[self: Self]() -> i32;
|
|
}
|
|
|
|
impl forall [template T:! type] T as GetX {
|
|
fn DoIt[self: Self]() -> i32 { return self.x; }
|
|
}
|
|
|
|
class C {
|
|
var x: i32;
|
|
}
|
|
|
|
fn Main() -> i32 {
|
|
var a: auto = {.x = 1, .y = 2};
|
|
var b: C = {.x = 3};
|
|
Print("{0}", a.(GetX.DoIt)());
|
|
Print("{0}", b.(GetX.DoIt)());
|
|
return 0;
|
|
}
|