Refactor CheckIsAllowedRedecl and stop function definition merging (#4800)

Rename `CheckIsAllowedRedecl` to `DiagnoseIfInvalidRedecl` to try to
better document behavior, and clean up comments.

This extends the no-merge-if-defined behavior to functions. It was
already the case for class/interface, and just added for impl, so if
anything functions were now inconsistent. I was kind of tempted to make
a helper for it, but I didn't think of a great structure/name to get
there: `DiagnoseRedef` isn't always called when it's a redefinition, for
example due to `extern` diagnostics, it's hard to combine.

Cleans up `is_defined` calls to rely more on `has_definition_started`,
removing some code paths that are unused since definitions aren't
merged.
This commit is contained in:
Jon Ross-Perkins
2025-01-14 21:13:21 +00:00
committed by GitHub
parent 5b70a3ea91
commit d958caaff3
15 changed files with 194 additions and 141 deletions
@@ -44,10 +44,16 @@ fn C(n: i32, m: i32) -> i32 = "int.sadd";
// CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [template]
// CHECK:STDOUT: %A.type: type = fn_type @A [template]
// CHECK:STDOUT: %A: %A.type = struct_value () [template]
// CHECK:STDOUT: %.type.b6a92a.1: type = fn_type @.1 [template]
// CHECK:STDOUT: %.d852be.1: %.type.b6a92a.1 = struct_value () [template]
// CHECK:STDOUT: %B.type: type = fn_type @B [template]
// CHECK:STDOUT: %B: %B.type = struct_value () [template]
// CHECK:STDOUT: %.type.b6a92a.2: type = fn_type @.2 [template]
// CHECK:STDOUT: %.d852be.2: %.type.b6a92a.2 = struct_value () [template]
// CHECK:STDOUT: %C.type: type = fn_type @C [template]
// CHECK:STDOUT: %C: %C.type = struct_value () [template]
// CHECK:STDOUT: %.type.b6a92a.3: type = fn_type @.3 [template]
// CHECK:STDOUT: %.d852be.3: %.type.b6a92a.3 = struct_value () [template]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: imports {
@@ -61,12 +67,12 @@ fn C(n: i32, m: i32) -> i32 = "int.sadd";
// CHECK:STDOUT: file {
// CHECK:STDOUT: package: <namespace> = namespace [template] {
// CHECK:STDOUT: .Core = imports.%Core
// CHECK:STDOUT: .A = %A.decl.loc11
// CHECK:STDOUT: .B = %B.decl.loc21
// CHECK:STDOUT: .C = %C.decl.loc31
// CHECK:STDOUT: .A = %A.decl
// CHECK:STDOUT: .B = %B.decl
// CHECK:STDOUT: .C = %C.decl
// CHECK:STDOUT: }
// CHECK:STDOUT: %Core.import = import Core
// CHECK:STDOUT: %A.decl.loc11: %A.type = fn_decl @A [template = constants.%A] {
// CHECK:STDOUT: %A.decl: %A.type = fn_decl @A [template = constants.%A] {
// CHECK:STDOUT: %n.patt: %i32 = binding_pattern n
// CHECK:STDOUT: %n.param_patt: %i32 = value_param_pattern %n.patt, runtime_param0
// CHECK:STDOUT: %m.patt: %i32 = binding_pattern m
@@ -76,22 +82,22 @@ fn C(n: i32, m: i32) -> i32 = "int.sadd";
// CHECK:STDOUT: } {
// CHECK:STDOUT: %int_32.loc11_25: Core.IntLiteral = int_value 32 [template = constants.%int_32]
// CHECK:STDOUT: %i32.loc11_25: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
// CHECK:STDOUT: %n.param.loc11: %i32 = value_param runtime_param0
// CHECK:STDOUT: %n.param: %i32 = value_param runtime_param0
// CHECK:STDOUT: %.loc11_9: type = splice_block %i32.loc11_9 [template = constants.%i32] {
// CHECK:STDOUT: %int_32.loc11_9: Core.IntLiteral = int_value 32 [template = constants.%int_32]
// CHECK:STDOUT: %i32.loc11_9: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
// CHECK:STDOUT: }
// CHECK:STDOUT: %n.loc11: %i32 = bind_name n, %n.param.loc11
// CHECK:STDOUT: %m.param.loc11: %i32 = value_param runtime_param1
// CHECK:STDOUT: %n: %i32 = bind_name n, %n.param
// CHECK:STDOUT: %m.param: %i32 = value_param runtime_param1
// CHECK:STDOUT: %.loc11_17: type = splice_block %i32.loc11_17 [template = constants.%i32] {
// CHECK:STDOUT: %int_32.loc11_17: Core.IntLiteral = int_value 32 [template = constants.%int_32]
// CHECK:STDOUT: %i32.loc11_17: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
// CHECK:STDOUT: }
// CHECK:STDOUT: %m.loc11: %i32 = bind_name m, %m.param.loc11
// CHECK:STDOUT: %return.param.loc11: ref %i32 = out_param runtime_param2
// CHECK:STDOUT: %return.loc11: ref %i32 = return_slot %return.param.loc11
// CHECK:STDOUT: %m: %i32 = bind_name m, %m.param
// CHECK:STDOUT: %return.param: ref %i32 = out_param runtime_param2
// CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
// CHECK:STDOUT: }
// CHECK:STDOUT: %A.decl.loc19: %A.type = fn_decl @A [template = constants.%A] {
// CHECK:STDOUT: %.decl.loc19: %.type.b6a92a.1 = fn_decl @.1 [template = constants.%.d852be.1] {
// CHECK:STDOUT: %n.patt: %i32 = binding_pattern n
// CHECK:STDOUT: %n.param_patt: %i32 = value_param_pattern %n.patt, runtime_param0
// CHECK:STDOUT: %m.patt: %i32 = binding_pattern m
@@ -101,22 +107,22 @@ fn C(n: i32, m: i32) -> i32 = "int.sadd";
// CHECK:STDOUT: } {
// CHECK:STDOUT: %int_32.loc19_25: Core.IntLiteral = int_value 32 [template = constants.%int_32]
// CHECK:STDOUT: %i32.loc19_25: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
// CHECK:STDOUT: %n.param.loc19: %i32 = value_param runtime_param0
// CHECK:STDOUT: %n.param: %i32 = value_param runtime_param0
// CHECK:STDOUT: %.loc19_9: type = splice_block %i32.loc19_9 [template = constants.%i32] {
// CHECK:STDOUT: %int_32.loc19_9: Core.IntLiteral = int_value 32 [template = constants.%int_32]
// CHECK:STDOUT: %i32.loc19_9: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
// CHECK:STDOUT: }
// CHECK:STDOUT: %n.loc19: %i32 = bind_name n, %n.param.loc19
// CHECK:STDOUT: %m.param.loc19: %i32 = value_param runtime_param1
// CHECK:STDOUT: %n: %i32 = bind_name n, %n.param
// CHECK:STDOUT: %m.param: %i32 = value_param runtime_param1
// CHECK:STDOUT: %.loc19_17: type = splice_block %i32.loc19_17 [template = constants.%i32] {
// CHECK:STDOUT: %int_32.loc19_17: Core.IntLiteral = int_value 32 [template = constants.%int_32]
// CHECK:STDOUT: %i32.loc19_17: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
// CHECK:STDOUT: }
// CHECK:STDOUT: %m.loc19: %i32 = bind_name m, %m.param.loc19
// CHECK:STDOUT: %return.param.loc19: ref %i32 = out_param runtime_param2
// CHECK:STDOUT: %return.loc19: ref %i32 = return_slot %return.param.loc19
// CHECK:STDOUT: %m: %i32 = bind_name m, %m.param
// CHECK:STDOUT: %return.param: ref %i32 = out_param runtime_param2
// CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
// CHECK:STDOUT: }
// CHECK:STDOUT: %B.decl.loc21: %B.type = fn_decl @B [template = constants.%B] {
// CHECK:STDOUT: %B.decl: %B.type = fn_decl @B [template = constants.%B] {
// CHECK:STDOUT: %n.patt: %i32 = binding_pattern n
// CHECK:STDOUT: %n.param_patt: %i32 = value_param_pattern %n.patt, runtime_param0
// CHECK:STDOUT: %m.patt: %i32 = binding_pattern m
@@ -126,22 +132,22 @@ fn C(n: i32, m: i32) -> i32 = "int.sadd";
// CHECK:STDOUT: } {
// CHECK:STDOUT: %int_32.loc21_25: Core.IntLiteral = int_value 32 [template = constants.%int_32]
// CHECK:STDOUT: %i32.loc21_25: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
// CHECK:STDOUT: %n.param.loc21: %i32 = value_param runtime_param0
// CHECK:STDOUT: %n.param: %i32 = value_param runtime_param0
// CHECK:STDOUT: %.loc21_9: type = splice_block %i32.loc21_9 [template = constants.%i32] {
// CHECK:STDOUT: %int_32.loc21_9: Core.IntLiteral = int_value 32 [template = constants.%int_32]
// CHECK:STDOUT: %i32.loc21_9: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
// CHECK:STDOUT: }
// CHECK:STDOUT: %n.loc21: %i32 = bind_name n, %n.param.loc21
// CHECK:STDOUT: %m.param.loc21: %i32 = value_param runtime_param1
// CHECK:STDOUT: %n: %i32 = bind_name n, %n.param
// CHECK:STDOUT: %m.param: %i32 = value_param runtime_param1
// CHECK:STDOUT: %.loc21_17: type = splice_block %i32.loc21_17 [template = constants.%i32] {
// CHECK:STDOUT: %int_32.loc21_17: Core.IntLiteral = int_value 32 [template = constants.%int_32]
// CHECK:STDOUT: %i32.loc21_17: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
// CHECK:STDOUT: }
// CHECK:STDOUT: %m.loc21: %i32 = bind_name m, %m.param.loc21
// CHECK:STDOUT: %return.param.loc21: ref %i32 = out_param runtime_param2
// CHECK:STDOUT: %return.loc21: ref %i32 = return_slot %return.param.loc21
// CHECK:STDOUT: %m: %i32 = bind_name m, %m.param
// CHECK:STDOUT: %return.param: ref %i32 = out_param runtime_param2
// CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
// CHECK:STDOUT: }
// CHECK:STDOUT: %B.decl.loc29: %B.type = fn_decl @B [template = constants.%B] {
// CHECK:STDOUT: %.decl.loc29: %.type.b6a92a.2 = fn_decl @.2 [template = constants.%.d852be.2] {
// CHECK:STDOUT: %n.patt: %i32 = binding_pattern n
// CHECK:STDOUT: %n.param_patt: %i32 = value_param_pattern %n.patt, runtime_param0
// CHECK:STDOUT: %m.patt: %i32 = binding_pattern m
@@ -151,22 +157,22 @@ fn C(n: i32, m: i32) -> i32 = "int.sadd";
// CHECK:STDOUT: } {
// CHECK:STDOUT: %int_32.loc29_25: Core.IntLiteral = int_value 32 [template = constants.%int_32]
// CHECK:STDOUT: %i32.loc29_25: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
// CHECK:STDOUT: %n.param.loc29: %i32 = value_param runtime_param0
// CHECK:STDOUT: %n.param: %i32 = value_param runtime_param0
// CHECK:STDOUT: %.loc29_9: type = splice_block %i32.loc29_9 [template = constants.%i32] {
// CHECK:STDOUT: %int_32.loc29_9: Core.IntLiteral = int_value 32 [template = constants.%int_32]
// CHECK:STDOUT: %i32.loc29_9: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
// CHECK:STDOUT: }
// CHECK:STDOUT: %n.loc29: %i32 = bind_name n, %n.param.loc29
// CHECK:STDOUT: %m.param.loc29: %i32 = value_param runtime_param1
// CHECK:STDOUT: %n: %i32 = bind_name n, %n.param
// CHECK:STDOUT: %m.param: %i32 = value_param runtime_param1
// CHECK:STDOUT: %.loc29_17: type = splice_block %i32.loc29_17 [template = constants.%i32] {
// CHECK:STDOUT: %int_32.loc29_17: Core.IntLiteral = int_value 32 [template = constants.%int_32]
// CHECK:STDOUT: %i32.loc29_17: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
// CHECK:STDOUT: }
// CHECK:STDOUT: %m.loc29: %i32 = bind_name m, %m.param.loc29
// CHECK:STDOUT: %return.param.loc29: ref %i32 = out_param runtime_param2
// CHECK:STDOUT: %return.loc29: ref %i32 = return_slot %return.param.loc29
// CHECK:STDOUT: %m: %i32 = bind_name m, %m.param
// CHECK:STDOUT: %return.param: ref %i32 = out_param runtime_param2
// CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
// CHECK:STDOUT: }
// CHECK:STDOUT: %C.decl.loc31: %C.type = fn_decl @C [template = constants.%C] {
// CHECK:STDOUT: %C.decl: %C.type = fn_decl @C [template = constants.%C] {
// CHECK:STDOUT: %n.patt: %i32 = binding_pattern n
// CHECK:STDOUT: %n.param_patt: %i32 = value_param_pattern %n.patt, runtime_param0
// CHECK:STDOUT: %m.patt: %i32 = binding_pattern m
@@ -176,22 +182,22 @@ fn C(n: i32, m: i32) -> i32 = "int.sadd";
// CHECK:STDOUT: } {
// CHECK:STDOUT: %int_32.loc31_25: Core.IntLiteral = int_value 32 [template = constants.%int_32]
// CHECK:STDOUT: %i32.loc31_25: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
// CHECK:STDOUT: %n.param.loc31: %i32 = value_param runtime_param0
// CHECK:STDOUT: %n.param: %i32 = value_param runtime_param0
// CHECK:STDOUT: %.loc31_9: type = splice_block %i32.loc31_9 [template = constants.%i32] {
// CHECK:STDOUT: %int_32.loc31_9: Core.IntLiteral = int_value 32 [template = constants.%int_32]
// CHECK:STDOUT: %i32.loc31_9: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
// CHECK:STDOUT: }
// CHECK:STDOUT: %n.loc31: %i32 = bind_name n, %n.param.loc31
// CHECK:STDOUT: %m.param.loc31: %i32 = value_param runtime_param1
// CHECK:STDOUT: %n: %i32 = bind_name n, %n.param
// CHECK:STDOUT: %m.param: %i32 = value_param runtime_param1
// CHECK:STDOUT: %.loc31_17: type = splice_block %i32.loc31_17 [template = constants.%i32] {
// CHECK:STDOUT: %int_32.loc31_17: Core.IntLiteral = int_value 32 [template = constants.%int_32]
// CHECK:STDOUT: %i32.loc31_17: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
// CHECK:STDOUT: }
// CHECK:STDOUT: %m.loc31: %i32 = bind_name m, %m.param.loc31
// CHECK:STDOUT: %return.param.loc31: ref %i32 = out_param runtime_param2
// CHECK:STDOUT: %return.loc31: ref %i32 = return_slot %return.param.loc31
// CHECK:STDOUT: %m: %i32 = bind_name m, %m.param
// CHECK:STDOUT: %return.param: ref %i32 = out_param runtime_param2
// CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
// CHECK:STDOUT: }
// CHECK:STDOUT: %C.decl.loc38: %C.type = fn_decl @C [template = constants.%C] {
// CHECK:STDOUT: %.decl.loc38: %.type.b6a92a.3 = fn_decl @.3 [template = constants.%.d852be.3] {
// CHECK:STDOUT: %n.patt: %i32 = binding_pattern n
// CHECK:STDOUT: %n.param_patt: %i32 = value_param_pattern %n.patt, runtime_param0
// CHECK:STDOUT: %m.patt: %i32 = binding_pattern m
@@ -201,34 +207,40 @@ fn C(n: i32, m: i32) -> i32 = "int.sadd";
// CHECK:STDOUT: } {
// CHECK:STDOUT: %int_32.loc38_25: Core.IntLiteral = int_value 32 [template = constants.%int_32]
// CHECK:STDOUT: %i32.loc38_25: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
// CHECK:STDOUT: %n.param.loc38: %i32 = value_param runtime_param0
// CHECK:STDOUT: %n.param: %i32 = value_param runtime_param0
// CHECK:STDOUT: %.loc38_9: type = splice_block %i32.loc38_9 [template = constants.%i32] {
// CHECK:STDOUT: %int_32.loc38_9: Core.IntLiteral = int_value 32 [template = constants.%int_32]
// CHECK:STDOUT: %i32.loc38_9: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
// CHECK:STDOUT: }
// CHECK:STDOUT: %n.loc38: %i32 = bind_name n, %n.param.loc38
// CHECK:STDOUT: %m.param.loc38: %i32 = value_param runtime_param1
// CHECK:STDOUT: %n: %i32 = bind_name n, %n.param
// CHECK:STDOUT: %m.param: %i32 = value_param runtime_param1
// CHECK:STDOUT: %.loc38_17: type = splice_block %i32.loc38_17 [template = constants.%i32] {
// CHECK:STDOUT: %int_32.loc38_17: Core.IntLiteral = int_value 32 [template = constants.%int_32]
// CHECK:STDOUT: %i32.loc38_17: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
// CHECK:STDOUT: }
// CHECK:STDOUT: %m.loc38: %i32 = bind_name m, %m.param.loc38
// CHECK:STDOUT: %return.param.loc38: ref %i32 = out_param runtime_param2
// CHECK:STDOUT: %return.loc38: ref %i32 = return_slot %return.param.loc38
// CHECK:STDOUT: %m: %i32 = bind_name m, %m.param
// CHECK:STDOUT: %return.param: ref %i32 = out_param runtime_param2
// CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @A(%n.param_patt: %i32, %m.param_patt: %i32) -> %i32 = "int.sadd" {
// CHECK:STDOUT: fn @A(%n.param_patt: %i32, %m.param_patt: %i32) -> %i32 = "int.sadd";
// CHECK:STDOUT:
// CHECK:STDOUT: fn @.1(%n.param_patt: %i32, %m.param_patt: %i32) -> %i32 {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %n.ref: %i32 = name_ref n, %n.loc19
// CHECK:STDOUT: %n.ref: %i32 = name_ref n, %n
// CHECK:STDOUT: return %n.ref
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @B(%n.param_patt: %i32, %m.param_patt: %i32) -> %i32 = "int.sadd" {
// CHECK:STDOUT: fn @B(%n.param_patt: %i32, %m.param_patt: %i32) -> %i32 {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %n.ref: %i32 = name_ref n, %n.loc21
// CHECK:STDOUT: %n.ref: %i32 = name_ref n, %n
// CHECK:STDOUT: return %n.ref
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @.2(%n.param_patt: %i32, %m.param_patt: %i32) -> %i32 = "int.sadd";
// CHECK:STDOUT:
// CHECK:STDOUT: fn @C(%n.param_patt: %i32, %m.param_patt: %i32) -> %i32 = "int.sadd";
// CHECK:STDOUT:
// CHECK:STDOUT: fn @.3(%n.param_patt: %i32, %m.param_patt: %i32) -> %i32 = "int.sadd";
// CHECK:STDOUT: