mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-06 08:54:47 +01:00
Basic support for generic bindings. (#3555)
This change adds a `BindSymbolicName` instruction for generic bindings, paralleling the existing `BindName`. A mechanism is also added to allow both kinds of binding to be accessed uniformly, for convenience in the case where the two different kinds of binding are treated the same. Generic bindings of type `type` are allowed to be used as types, although no operations are provided for such types. For now lowering treats these types as empty structs, which seems like a reasonable lowering for non-monomorphized unconstrained types.
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
// 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
|
||||
|
||||
fn F(T:! type, U:! type) {
|
||||
var p: T*;
|
||||
// CHECK:STDERR: fail_type_param_mismatch.carbon:[[@LINE+3]]:3: ERROR: Cannot implicitly convert from `T` to `U`.
|
||||
// CHECK:STDERR: let n: U = *p;
|
||||
// CHECK:STDERR: ^~~~~~~~~~~~~~
|
||||
let n: U = *p;
|
||||
}
|
||||
|
||||
// CHECK:STDOUT: --- fail_type_param_mismatch.carbon
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: file {
|
||||
// CHECK:STDOUT: package: <namespace> = namespace {.F = %F}
|
||||
// CHECK:STDOUT: %F: <function> = fn_decl @F
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: fn @F(%T: type, %U: type) {
|
||||
// CHECK:STDOUT: !entry:
|
||||
// CHECK:STDOUT: %T.ref: type = name_ref T, %T
|
||||
// CHECK:STDOUT: %.loc8: type = ptr_type T
|
||||
// CHECK:STDOUT: %p.var: ref T* = var p
|
||||
// CHECK:STDOUT: %p: ref T* = bind_name p, %p.var
|
||||
// CHECK:STDOUT: %U.ref: type = name_ref U, %U
|
||||
// CHECK:STDOUT: %p.ref: ref T* = name_ref p, %p
|
||||
// CHECK:STDOUT: %.loc12_15: T* = bind_value %p.ref
|
||||
// CHECK:STDOUT: %.loc12_14: ref T = deref %.loc12_15
|
||||
// CHECK:STDOUT: %n: U = bind_name n, <error>
|
||||
// CHECK:STDOUT: return
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
@@ -0,0 +1,33 @@
|
||||
// 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
|
||||
|
||||
fn F(T:! type) {
|
||||
var p: T*;
|
||||
let n: T = *p;
|
||||
}
|
||||
|
||||
// CHECK:STDOUT: --- type_param.carbon
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: file {
|
||||
// CHECK:STDOUT: package: <namespace> = namespace {.F = %F}
|
||||
// CHECK:STDOUT: %F: <function> = fn_decl @F
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: fn @F(%T: type) {
|
||||
// CHECK:STDOUT: !entry:
|
||||
// CHECK:STDOUT: %T.ref.loc8: type = name_ref T, %T
|
||||
// CHECK:STDOUT: %.loc8: type = ptr_type T
|
||||
// CHECK:STDOUT: %p.var: ref T* = var p
|
||||
// CHECK:STDOUT: %p: ref T* = bind_name p, %p.var
|
||||
// CHECK:STDOUT: %T.ref.loc9: type = name_ref T, %T
|
||||
// CHECK:STDOUT: %p.ref: ref T* = name_ref p, %p
|
||||
// CHECK:STDOUT: %.loc9_15: T* = bind_value %p.ref
|
||||
// CHECK:STDOUT: %.loc9_14.1: ref T = deref %.loc9_15
|
||||
// CHECK:STDOUT: %.loc9_14.2: T = bind_value %.loc9_14.1
|
||||
// CHECK:STDOUT: %n: T = bind_name n, %.loc9_14.2
|
||||
// CHECK:STDOUT: return
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
Reference in New Issue
Block a user