Model var as a pattern operator (#4720)

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
This commit is contained in:
Geoff Romer
2025-01-17 17:51:34 +00:00
committed by GitHub
co-authored by Richard Smith
parent 6572da7314
commit 13434f0e8a
510 changed files with 11819 additions and 3352 deletions
+120 -10
View File
@@ -18,10 +18,10 @@ class EmptyParams() {}
var v: NoParams;
// CHECK:STDERR: fail_empty_params.carbon:[[@LINE+7]]:1: error: cannot implicitly convert from `NoParams` to `EmptyParams()` [ImplicitAsConversionFailure]
// CHECK:STDERR: var w: EmptyParams() = v;
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR: fail_empty_params.carbon:[[@LINE+4]]:1: note: type `NoParams` does not implement interface `Core.ImplicitAs(EmptyParams())` [MissingImplInMemberAccessNote]
// CHECK:STDERR: var w: EmptyParams() = v;
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
var w: EmptyParams() = v;
@@ -39,10 +39,10 @@ var v: Outer({}*);
// TODO: It would be nice to include the `Outer({}*).` prefix in the name of `Inner`.
// CHECK:STDERR: fail_nested.carbon:[[@LINE+7]]:1: error: cannot implicitly convert from `Outer({}*)` to `Inner({.a: i32}*)` [ImplicitAsConversionFailure]
// CHECK:STDERR: var w: Outer({}*).Inner({.a: i32}*) = v;
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR: fail_nested.carbon:[[@LINE+4]]:1: note: type `Outer({}*)` does not implement interface `Core.ImplicitAs(Inner({.a: i32}*))` [MissingImplInMemberAccessNote]
// CHECK:STDERR: var w: Outer({}*).Inner({.a: i32}*) = v;
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
var w: Outer({}*).Inner({.a: i32}*) = v;
@@ -54,10 +54,10 @@ class C(N:! i32) {}
// CHECK:STDERR: fail_int_value.carbon:[[@LINE+7]]:1: error: cannot implicitly convert from `()` to `C(123)` [ImplicitAsConversionFailure]
// CHECK:STDERR: var v: C(123) = ();
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~
// CHECK:STDERR: ^~~~~~~~~~~~~
// CHECK:STDERR: fail_int_value.carbon:[[@LINE+4]]:1: note: type `()` does not implement interface `Core.ImplicitAs(C(123))` [MissingImplInMemberAccessNote]
// CHECK:STDERR: var v: C(123) = ();
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~
// CHECK:STDERR: ^~~~~~~~~~~~~
// CHECK:STDERR:
var v: C(123) = ();
@@ -118,9 +118,22 @@ var g: E({.a = 1, .b = 2}) = {} as E({.a = 3, .b = 4} as D);
// CHECK:STDOUT: %Core.import = import Core
// CHECK:STDOUT: %NoParams.decl: type = class_decl @NoParams [template = constants.%NoParams] {} {}
// CHECK:STDOUT: %EmptyParams.decl: %EmptyParams.type = class_decl @EmptyParams [template = constants.%EmptyParams.generic] {} {}
// CHECK:STDOUT: name_binding_decl {
// CHECK:STDOUT: %v.patt: %NoParams = binding_pattern v
// CHECK:STDOUT: %.loc7: %NoParams = var_pattern %v.patt
// CHECK:STDOUT: }
// CHECK:STDOUT: %v.var: ref %NoParams = var v
// CHECK:STDOUT: %NoParams.ref: type = name_ref NoParams, %NoParams.decl [template = constants.%NoParams]
// CHECK:STDOUT: %v: ref %NoParams = bind_name v, %v.var
// CHECK:STDOUT: name_binding_decl {
// CHECK:STDOUT: %w.patt: %EmptyParams = binding_pattern w
// CHECK:STDOUT: %.loc15_1: %EmptyParams = var_pattern %w.patt
// CHECK:STDOUT: }
// CHECK:STDOUT: %w.var: ref %EmptyParams = var w
// CHECK:STDOUT: %.loc15_20: type = splice_block %EmptyParams [template = constants.%EmptyParams] {
// CHECK:STDOUT: %EmptyParams.ref: %EmptyParams.type = name_ref EmptyParams, %EmptyParams.decl [template = constants.%EmptyParams.generic]
// CHECK:STDOUT: %EmptyParams: type = class_type @EmptyParams [template = constants.%EmptyParams]
// CHECK:STDOUT: }
// CHECK:STDOUT: %w: ref %EmptyParams = bind_name w, %w.var
// CHECK:STDOUT: }
// CHECK:STDOUT:
@@ -190,9 +203,38 @@ var g: E({.a = 1, .b = 2}) = {} as E({.a = 3, .b = 4} as D);
// CHECK:STDOUT: %T.param: type = value_param runtime_param<invalid>
// CHECK:STDOUT: %T.loc4_13.1: type = bind_symbolic_name T, 0, %T.param [symbolic = %T.loc4_13.2 (constants.%T)]
// CHECK:STDOUT: }
// CHECK:STDOUT: name_binding_decl {
// CHECK:STDOUT: %v.patt: %Outer.614 = binding_pattern v
// CHECK:STDOUT: %.loc9_1: %Outer.614 = var_pattern %v.patt
// CHECK:STDOUT: }
// CHECK:STDOUT: %v.var: ref %Outer.614 = var v
// CHECK:STDOUT: %.loc9_17: type = splice_block %Outer.loc9 [template = constants.%Outer.614] {
// CHECK:STDOUT: %Outer.ref.loc9: %Outer.type = name_ref Outer, %Outer.decl [template = constants.%Outer.generic]
// CHECK:STDOUT: %.loc9_15: %empty_struct_type = struct_literal ()
// CHECK:STDOUT: %.loc9_16: type = converted %.loc9_15, constants.%empty_struct_type [template = constants.%empty_struct_type]
// CHECK:STDOUT: %ptr.loc9: type = ptr_type %empty_struct_type [template = constants.%ptr.c28]
// CHECK:STDOUT: %Outer.loc9: type = class_type @Outer, @Outer(constants.%ptr.c28) [template = constants.%Outer.614]
// CHECK:STDOUT: }
// CHECK:STDOUT: %v: ref %Outer.614 = bind_name v, %v.var
// CHECK:STDOUT: name_binding_decl {
// CHECK:STDOUT: %w.patt: %Inner.277 = binding_pattern w
// CHECK:STDOUT: %.loc19_1: %Inner.277 = var_pattern %w.patt
// CHECK:STDOUT: }
// CHECK:STDOUT: %w.var: ref %Inner.277 = var w
// CHECK:STDOUT: %.loc19_35: type = splice_block %Inner [template = constants.%Inner.277] {
// CHECK:STDOUT: %Outer.ref.loc19: %Outer.type = name_ref Outer, %Outer.decl [template = constants.%Outer.generic]
// CHECK:STDOUT: %.loc19_15: %empty_struct_type = struct_literal ()
// CHECK:STDOUT: %.loc19_16: type = converted %.loc19_15, constants.%empty_struct_type [template = constants.%empty_struct_type]
// CHECK:STDOUT: %ptr.loc19_16: type = ptr_type %empty_struct_type [template = constants.%ptr.c28]
// CHECK:STDOUT: %Outer.loc19: type = class_type @Outer, @Outer(constants.%ptr.c28) [template = constants.%Outer.614]
// CHECK:STDOUT: %.loc19_18: %Inner.type.5d2 = specific_constant @Outer.%Inner.decl, @Outer(constants.%ptr.c28) [template = constants.%Inner.generic.8e6]
// CHECK:STDOUT: %Inner.ref: %Inner.type.5d2 = name_ref Inner, %.loc19_18 [template = constants.%Inner.generic.8e6]
// CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
// CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
// CHECK:STDOUT: %struct_type.a: type = struct_type {.a: %i32} [template = constants.%struct_type.a]
// CHECK:STDOUT: %ptr.loc19_34: type = ptr_type %struct_type.a [template = constants.%ptr.1bb]
// CHECK:STDOUT: %Inner: type = class_type @Inner, @Inner(constants.%ptr.c28, constants.%ptr.1bb) [template = constants.%Inner.277]
// CHECK:STDOUT: }
// CHECK:STDOUT: %w: ref %Inner.277 = bind_name w, %w.var
// CHECK:STDOUT: }
// CHECK:STDOUT:
@@ -277,6 +319,13 @@ var g: E({.a = 1, .b = 2}) = {} as E({.a = 3, .b = 4} as D);
// CHECK:STDOUT: %C.generic: %C.type = struct_value () [template]
// CHECK:STDOUT: %empty_struct_type: type = struct_type {} [template]
// CHECK:STDOUT: %complete_type.357: <witness> = complete_type_witness %empty_struct_type [template]
// CHECK:STDOUT: %int_123.fff: Core.IntLiteral = int_value 123 [template]
// CHECK:STDOUT: %Convert.type.1b6: type = fn_type @Convert.1, @ImplicitAs(%i32) [template]
// CHECK:STDOUT: %impl_witness.d39: <witness> = impl_witness (imports.%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: %Convert.bound: <bound method> = bound_method %int_123.fff, %Convert.956 [template]
// CHECK:STDOUT: %Convert.specific_fn: <specific function> = specific_function %Convert.bound, @Convert.2(%int_32) [template]
// CHECK:STDOUT: %int_123.f7f: %i32 = int_value 123 [template]
// CHECK:STDOUT: %C.4c3: type = class_type @C, @C(%int_123.f7f) [template]
// CHECK:STDOUT: }
@@ -308,7 +357,22 @@ var g: E({.a = 1, .b = 2}) = {} as E({.a = 3, .b = 4} as D);
// CHECK:STDOUT: }
// CHECK:STDOUT: %N.loc4_9.1: %i32 = bind_symbolic_name N, 0, %N.param [symbolic = %N.loc4_9.2 (constants.%N.51e)]
// CHECK:STDOUT: }
// CHECK:STDOUT: name_binding_decl {
// CHECK:STDOUT: %v.patt: %C.4c3 = binding_pattern v
// CHECK:STDOUT: %.loc13_1: %C.4c3 = var_pattern %v.patt
// CHECK:STDOUT: }
// CHECK:STDOUT: %v.var: ref %C.4c3 = var v
// CHECK:STDOUT: %.loc13_13.1: type = splice_block %C [template = constants.%C.4c3] {
// CHECK:STDOUT: %C.ref: %C.type = name_ref C, %C.decl [template = constants.%C.generic]
// CHECK:STDOUT: %int_123: Core.IntLiteral = int_value 123 [template = constants.%int_123.fff]
// CHECK:STDOUT: %impl.elem0: %Convert.type.1b6 = impl_witness_access constants.%impl_witness.d39, element0 [template = constants.%Convert.956]
// CHECK:STDOUT: %Convert.bound: <bound method> = bound_method %int_123, %impl.elem0 [template = constants.%Convert.bound]
// CHECK:STDOUT: %Convert.specific_fn: <specific function> = specific_function %Convert.bound, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn]
// CHECK:STDOUT: %int.convert_checked: init %i32 = call %Convert.specific_fn(%int_123) [template = constants.%int_123.f7f]
// CHECK:STDOUT: %.loc13_13.2: %i32 = value_of_initializer %int.convert_checked [template = constants.%int_123.f7f]
// CHECK:STDOUT: %.loc13_13.3: %i32 = converted %int_123, %.loc13_13.2 [template = constants.%int_123.f7f]
// CHECK:STDOUT: %C: type = class_type @C, @C(constants.%int_123.f7f) [template = constants.%C.4c3]
// CHECK:STDOUT: }
// CHECK:STDOUT: %v: ref %C.4c3 = bind_name v, %v.var
// CHECK:STDOUT: }
// CHECK:STDOUT:
@@ -327,7 +391,7 @@ var g: E({.a = 1, .b = 2}) = {} as E({.a = 3, .b = 4} as D);
// CHECK:STDOUT: fn @__global_init() {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %.loc13_18: %empty_tuple.type = tuple_literal ()
// CHECK:STDOUT: %.loc13_19: %C.4c3 = converted %.loc13_18, <error> [template = <error>]
// CHECK:STDOUT: %.loc13_1: %C.4c3 = converted %.loc13_18, <error> [template = <error>]
// CHECK:STDOUT: assign file.%v.var, <error>
// CHECK:STDOUT: return
// CHECK:STDOUT: }
@@ -359,11 +423,20 @@ var g: E({.a = 1, .b = 2}) = {} as E({.a = 3, .b = 4} as D);
// CHECK:STDOUT: %E.generic: %E.type = struct_value () [template]
// CHECK:STDOUT: %empty_struct_type: type = struct_type {} [template]
// CHECK:STDOUT: %complete_type.357: <witness> = complete_type_witness %empty_struct_type [template]
// CHECK:STDOUT: %int_1.5b8: Core.IntLiteral = int_value 1 [template]
// CHECK:STDOUT: %int_2.ecc: Core.IntLiteral = int_value 2 [template]
// CHECK:STDOUT: %struct_type.a.b.cfd: type = struct_type {.a: Core.IntLiteral, .b: Core.IntLiteral} [template]
// CHECK:STDOUT: %Convert.type.1b6: type = fn_type @Convert.1, @ImplicitAs(%i32) [template]
// CHECK:STDOUT: %impl_witness.d39: <witness> = impl_witness (imports.%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: %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]
// CHECK:STDOUT: %Convert.bound.ef9: <bound method> = bound_method %int_2.ecc, %Convert.956 [template]
// CHECK:STDOUT: %Convert.specific_fn.787: <specific function> = specific_function %Convert.bound.ef9, @Convert.2(%int_32) [template]
// CHECK:STDOUT: %int_2.ef8: %i32 = int_value 2 [template]
// CHECK:STDOUT: %D.val.413: %D = struct_value (%int_1.5d2, %int_2.ef8) [template]
// CHECK:STDOUT: %int_3.1ba: Core.IntLiteral = int_value 3 [template]
// CHECK:STDOUT: %int_4.0c1: Core.IntLiteral = int_value 4 [template]
// CHECK:STDOUT: %Convert.bound.b30: <bound method> = bound_method %int_3.1ba, %Convert.956 [template]
@@ -401,13 +474,50 @@ var g: E({.a = 1, .b = 2}) = {} as E({.a = 3, .b = 4} as D);
// CHECK:STDOUT: %D.ref: type = name_ref D, file.%D.decl [template = constants.%D]
// CHECK:STDOUT: %F.loc9_9.1: %D = bind_symbolic_name F, 0, %F.param [symbolic = %F.loc9_9.2 (constants.%F)]
// CHECK:STDOUT: }
// CHECK:STDOUT: name_binding_decl {
// CHECK:STDOUT: %g.patt: <error> = binding_pattern g
// CHECK:STDOUT: %.loc25_1: <error> = var_pattern %g.patt
// CHECK:STDOUT: }
// CHECK:STDOUT: %g.var: ref <error> = var g
// CHECK:STDOUT: %g: ref <error> = bind_name g, %g.var
// CHECK:STDOUT: %.1: <error> = splice_block <error> [template = <error>] {
// CHECK:STDOUT: %E.ref: %E.type = name_ref E, %E.decl [template = constants.%E.generic]
// CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [template = constants.%int_1.5b8]
// CHECK:STDOUT: %int_2: Core.IntLiteral = int_value 2 [template = constants.%int_2.ecc]
// CHECK:STDOUT: %.loc25_25.1: %struct_type.a.b.cfd = struct_literal (%int_1, %int_2)
// CHECK:STDOUT: %impl.elem0.loc25_25.1: %Convert.type.1b6 = impl_witness_access constants.%impl_witness.d39, element0 [template = constants.%Convert.956]
// CHECK:STDOUT: %Convert.bound.loc25_25.1: <bound method> = bound_method %int_1, %impl.elem0.loc25_25.1 [template = constants.%Convert.bound.ab5]
// CHECK:STDOUT: %Convert.specific_fn.loc25_25.1: <specific function> = specific_function %Convert.bound.loc25_25.1, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.70c]
// CHECK:STDOUT: %int.convert_checked.loc25_25.1: init %i32 = call %Convert.specific_fn.loc25_25.1(%int_1) [template = constants.%int_1.5d2]
// CHECK:STDOUT: %.loc25_25.2: init %i32 = converted %int_1, %int.convert_checked.loc25_25.1 [template = constants.%int_1.5d2]
// CHECK:STDOUT: %.loc25_25.3: ref %D = temporary_storage
// CHECK:STDOUT: %.loc25_25.4: ref %i32 = class_element_access %.loc25_25.3, element0
// CHECK:STDOUT: %.loc25_25.5: init %i32 = initialize_from %.loc25_25.2 to %.loc25_25.4 [template = constants.%int_1.5d2]
// CHECK:STDOUT: %impl.elem0.loc25_25.2: %Convert.type.1b6 = impl_witness_access constants.%impl_witness.d39, element0 [template = constants.%Convert.956]
// CHECK:STDOUT: %Convert.bound.loc25_25.2: <bound method> = bound_method %int_2, %impl.elem0.loc25_25.2 [template = constants.%Convert.bound.ef9]
// CHECK:STDOUT: %Convert.specific_fn.loc25_25.2: <specific function> = specific_function %Convert.bound.loc25_25.2, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn.787]
// CHECK:STDOUT: %int.convert_checked.loc25_25.2: init %i32 = call %Convert.specific_fn.loc25_25.2(%int_2) [template = constants.%int_2.ef8]
// CHECK:STDOUT: %.loc25_25.6: init %i32 = converted %int_2, %int.convert_checked.loc25_25.2 [template = constants.%int_2.ef8]
// CHECK:STDOUT: %.loc25_25.7: ref %i32 = class_element_access %.loc25_25.3, element1
// CHECK:STDOUT: %.loc25_25.8: init %i32 = initialize_from %.loc25_25.6 to %.loc25_25.7 [template = constants.%int_2.ef8]
// CHECK:STDOUT: %.loc25_25.9: init %D = class_init (%.loc25_25.5, %.loc25_25.8), %.loc25_25.3 [template = constants.%D.val.413]
// CHECK:STDOUT: %.loc25_25.10: ref %D = temporary %.loc25_25.3, %.loc25_25.9
// CHECK:STDOUT: %.loc25_26.1: ref %D = converted %.loc25_25.1, %.loc25_25.10
// CHECK:STDOUT: %.loc25_26.2: %D = bind_value %.loc25_26.1
// CHECK:STDOUT: }
// CHECK:STDOUT: %g: <error> = bind_name g, <error>
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: class @D {
// CHECK:STDOUT: %.loc5: %D.elem = field_decl a, element0 [template]
// CHECK:STDOUT: %.loc6: %D.elem = field_decl b, element1 [template]
// CHECK:STDOUT: %.loc5_8: %D.elem = field_decl a, element0 [template]
// CHECK:STDOUT: name_binding_decl {
// CHECK:STDOUT: %.loc5_3: %D.elem = var_pattern %.loc5_8
// CHECK:STDOUT: }
// CHECK:STDOUT: %.var.loc5: ref %D.elem = var <invalid>
// CHECK:STDOUT: %.loc6_8: %D.elem = field_decl b, element1 [template]
// CHECK:STDOUT: name_binding_decl {
// CHECK:STDOUT: %.loc6_3: %D.elem = var_pattern %.loc6_8
// CHECK:STDOUT: }
// CHECK:STDOUT: %.var.loc6: ref %D.elem = var <invalid>
// CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %struct_type.a.b.501 [template = constants.%complete_type.705]
// CHECK:STDOUT: complete_type_witness = %complete_type
// CHECK:STDOUT: }