mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
Support accessing associated functions by member access into facets (#4872)
For an expression such as `(Type as Interface).AssocFn()`, track the `Self` type `Type` in the result of the member access so that it's available when checking the function call. This introduces a new kind of type, `ImplFunctionType`, that represents the type of a function that is expected within an impl, modeled as the type of the function within the interface plus a value to use as `Self`. Calls to values of this type behave like calls to the underlying function except that the `Self` parameter is pre-bound to the self type from the facet. In order to support this, fix an issue where the imported list of generic bindings lost their association with their enclosing generic. This adds a little complexity to `import_ref`, including a new recursive cycle that I intend to address in a follow-up PR. --------- Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
This commit is contained in:
co-authored by
Jon Ross-Perkins
parent
dcfccd3187
commit
fcfb1345d5
+19
-16
@@ -31,10 +31,13 @@ fn G() -> i32 {
|
||||
// CHECK:STDOUT: %int_2.ecc: Core.IntLiteral = int_value 2 [template]
|
||||
// CHECK:STDOUT: %tuple.type: type = tuple_type (Core.IntLiteral, Core.IntLiteral, Core.IntLiteral) [template]
|
||||
// CHECK:STDOUT: %int_0: Core.IntLiteral = int_value 0 [template]
|
||||
// CHECK:STDOUT: %ImplicitAs.type.205: type = facet_type <@ImplicitAs, @ImplicitAs(%i32)> [template]
|
||||
// CHECK:STDOUT: %Convert.type.1b6: type = fn_type @Convert.1, @ImplicitAs(%i32) [template]
|
||||
// CHECK:STDOUT: %impl_witness.d39: <witness> = impl_witness (imports.%Core.import_ref.a5b), @impl.1(%int_32) [template]
|
||||
// CHECK:STDOUT: %Convert.type.035: type = fn_type @Convert.2, @impl.1(%int_32) [template]
|
||||
// CHECK:STDOUT: %Convert.956: %Convert.type.035 = struct_value () [template]
|
||||
// CHECK:STDOUT: %ImplicitAs.facet: %ImplicitAs.type.205 = facet_value Core.IntLiteral, %impl_witness.d39 [template]
|
||||
// CHECK:STDOUT: %.a0b: type = fn_type_with_self_type %Convert.type.1b6, %ImplicitAs.facet [template]
|
||||
// CHECK:STDOUT: %Convert.bound.ab5: <bound method> = bound_method %int_1.5b8, %Convert.956 [template]
|
||||
// CHECK:STDOUT: %Convert.specific_fn.70c: <specific function> = specific_function %Convert.bound.ab5, @Convert.2(%int_32) [template]
|
||||
// CHECK:STDOUT: %int_1.5d2: %i32 = int_value 1 [template]
|
||||
@@ -121,27 +124,27 @@ fn G() -> i32 {
|
||||
// CHECK:STDOUT: %int_3: Core.IntLiteral = int_value 3 [template = constants.%int_3.1ba]
|
||||
// CHECK:STDOUT: %.loc16_20.1: %tuple.type = tuple_literal (%int_1.loc16_13, %int_2.loc16_16, %int_3)
|
||||
// CHECK:STDOUT: %int_1.loc16_23: Core.IntLiteral = int_value 1 [template = constants.%int_1.5b8]
|
||||
// CHECK:STDOUT: %impl.elem0.loc16_20.1: %Convert.type.1b6 = impl_witness_access constants.%impl_witness.d39, element0 [template = constants.%Convert.956]
|
||||
// CHECK:STDOUT: %Convert.bound.loc16_20.1: <bound method> = bound_method %int_1.loc16_13, %impl.elem0.loc16_20.1 [template = constants.%Convert.bound.ab5]
|
||||
// CHECK:STDOUT: %Convert.specific_fn.loc16_20.1: <specific function> = specific_function %Convert.bound.loc16_20.1, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.70c]
|
||||
// CHECK:STDOUT: %int.convert_checked.loc16_20.1: init %i32 = call %Convert.specific_fn.loc16_20.1(%int_1.loc16_13) [template = constants.%int_1.5d2]
|
||||
// CHECK:STDOUT: %impl.elem0.loc16_20.1: %.a0b = impl_witness_access constants.%impl_witness.d39, element0 [template = constants.%Convert.956]
|
||||
// CHECK:STDOUT: %bound_method.loc16_20.1: <bound method> = bound_method %int_1.loc16_13, %impl.elem0.loc16_20.1 [template = constants.%Convert.bound.ab5]
|
||||
// CHECK:STDOUT: %specific_fn.loc16_20.1: <specific function> = specific_function %bound_method.loc16_20.1, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.70c]
|
||||
// CHECK:STDOUT: %int.convert_checked.loc16_20.1: init %i32 = call %specific_fn.loc16_20.1(%int_1.loc16_13) [template = constants.%int_1.5d2]
|
||||
// CHECK:STDOUT: %.loc16_20.2: init %i32 = converted %int_1.loc16_13, %int.convert_checked.loc16_20.1 [template = constants.%int_1.5d2]
|
||||
// CHECK:STDOUT: %.loc16_20.3: ref %array_type = temporary_storage
|
||||
// CHECK:STDOUT: %int_0: Core.IntLiteral = int_value 0 [template = constants.%int_0]
|
||||
// CHECK:STDOUT: %.loc16_20.4: ref %i32 = array_index %.loc16_20.3, %int_0
|
||||
// CHECK:STDOUT: %.loc16_20.5: init %i32 = initialize_from %.loc16_20.2 to %.loc16_20.4 [template = constants.%int_1.5d2]
|
||||
// CHECK:STDOUT: %impl.elem0.loc16_20.2: %Convert.type.1b6 = impl_witness_access constants.%impl_witness.d39, element0 [template = constants.%Convert.956]
|
||||
// CHECK:STDOUT: %Convert.bound.loc16_20.2: <bound method> = bound_method %int_2.loc16_16, %impl.elem0.loc16_20.2 [template = constants.%Convert.bound.ef9]
|
||||
// CHECK:STDOUT: %Convert.specific_fn.loc16_20.2: <specific function> = specific_function %Convert.bound.loc16_20.2, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.787]
|
||||
// CHECK:STDOUT: %int.convert_checked.loc16_20.2: init %i32 = call %Convert.specific_fn.loc16_20.2(%int_2.loc16_16) [template = constants.%int_2.ef8]
|
||||
// CHECK:STDOUT: %impl.elem0.loc16_20.2: %.a0b = impl_witness_access constants.%impl_witness.d39, element0 [template = constants.%Convert.956]
|
||||
// CHECK:STDOUT: %bound_method.loc16_20.2: <bound method> = bound_method %int_2.loc16_16, %impl.elem0.loc16_20.2 [template = constants.%Convert.bound.ef9]
|
||||
// CHECK:STDOUT: %specific_fn.loc16_20.2: <specific function> = specific_function %bound_method.loc16_20.2, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.787]
|
||||
// CHECK:STDOUT: %int.convert_checked.loc16_20.2: init %i32 = call %specific_fn.loc16_20.2(%int_2.loc16_16) [template = constants.%int_2.ef8]
|
||||
// CHECK:STDOUT: %.loc16_20.6: init %i32 = converted %int_2.loc16_16, %int.convert_checked.loc16_20.2 [template = constants.%int_2.ef8]
|
||||
// CHECK:STDOUT: %int_1.loc16_20: Core.IntLiteral = int_value 1 [template = constants.%int_1.5b8]
|
||||
// CHECK:STDOUT: %.loc16_20.7: ref %i32 = array_index %.loc16_20.3, %int_1.loc16_20
|
||||
// CHECK:STDOUT: %.loc16_20.8: init %i32 = initialize_from %.loc16_20.6 to %.loc16_20.7 [template = constants.%int_2.ef8]
|
||||
// CHECK:STDOUT: %impl.elem0.loc16_20.3: %Convert.type.1b6 = impl_witness_access constants.%impl_witness.d39, element0 [template = constants.%Convert.956]
|
||||
// CHECK:STDOUT: %Convert.bound.loc16_20.3: <bound method> = bound_method %int_3, %impl.elem0.loc16_20.3 [template = constants.%Convert.bound.b30]
|
||||
// CHECK:STDOUT: %Convert.specific_fn.loc16_20.3: <specific function> = specific_function %Convert.bound.loc16_20.3, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.b42]
|
||||
// CHECK:STDOUT: %int.convert_checked.loc16_20.3: init %i32 = call %Convert.specific_fn.loc16_20.3(%int_3) [template = constants.%int_3.822]
|
||||
// CHECK:STDOUT: %impl.elem0.loc16_20.3: %.a0b = impl_witness_access constants.%impl_witness.d39, element0 [template = constants.%Convert.956]
|
||||
// CHECK:STDOUT: %bound_method.loc16_20.3: <bound method> = bound_method %int_3, %impl.elem0.loc16_20.3 [template = constants.%Convert.bound.b30]
|
||||
// CHECK:STDOUT: %specific_fn.loc16_20.3: <specific function> = specific_function %bound_method.loc16_20.3, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.b42]
|
||||
// CHECK:STDOUT: %int.convert_checked.loc16_20.3: init %i32 = call %specific_fn.loc16_20.3(%int_3) [template = constants.%int_3.822]
|
||||
// CHECK:STDOUT: %.loc16_20.9: init %i32 = converted %int_3, %int.convert_checked.loc16_20.3 [template = constants.%int_3.822]
|
||||
// CHECK:STDOUT: %int_2.loc16_20: Core.IntLiteral = int_value 2 [template = constants.%int_2.ecc]
|
||||
// CHECK:STDOUT: %.loc16_20.10: ref %i32 = array_index %.loc16_20.3, %int_2.loc16_20
|
||||
@@ -150,10 +153,10 @@ fn G() -> i32 {
|
||||
// CHECK:STDOUT: %.loc16_20.13: init %array_type = converted %.loc16_20.1, %.loc16_20.12 [template = constants.%array]
|
||||
// CHECK:STDOUT: %.loc16_20.14: ref %array_type = temporary %.loc16_20.3, %.loc16_20.13
|
||||
// CHECK:STDOUT: %.loc16_20.15: %array_type = bind_value %.loc16_20.14
|
||||
// CHECK:STDOUT: %impl.elem0.loc16_23: %Convert.type.1b6 = impl_witness_access constants.%impl_witness.d39, element0 [template = constants.%Convert.956]
|
||||
// CHECK:STDOUT: %Convert.bound.loc16_23: <bound method> = bound_method %int_1.loc16_23, %impl.elem0.loc16_23 [template = constants.%Convert.bound.ab5]
|
||||
// CHECK:STDOUT: %Convert.specific_fn.loc16_23: <specific function> = specific_function %Convert.bound.loc16_23, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.70c]
|
||||
// CHECK:STDOUT: %int.convert_checked.loc16_23: init %i32 = call %Convert.specific_fn.loc16_23(%int_1.loc16_23) [template = constants.%int_1.5d2]
|
||||
// CHECK:STDOUT: %impl.elem0.loc16_23: %.a0b = impl_witness_access constants.%impl_witness.d39, element0 [template = constants.%Convert.956]
|
||||
// CHECK:STDOUT: %bound_method.loc16_23: <bound method> = bound_method %int_1.loc16_23, %impl.elem0.loc16_23 [template = constants.%Convert.bound.ab5]
|
||||
// CHECK:STDOUT: %specific_fn.loc16_23: <specific function> = specific_function %bound_method.loc16_23, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.70c]
|
||||
// CHECK:STDOUT: %int.convert_checked.loc16_23: init %i32 = call %specific_fn.loc16_23(%int_1.loc16_23) [template = constants.%int_1.5d2]
|
||||
// CHECK:STDOUT: %.loc16_23.1: %i32 = value_of_initializer %int.convert_checked.loc16_23 [template = constants.%int_1.5d2]
|
||||
// CHECK:STDOUT: %.loc16_23.2: %i32 = converted %int_1.loc16_23, %.loc16_23.1 [template = constants.%int_1.5d2]
|
||||
// CHECK:STDOUT: %F.call: init %i32 = call %F.ref(%.loc16_20.15, %.loc16_23.2)
|
||||
|
||||
Reference in New Issue
Block a user