Files
carbon-lang/toolchain/check/testdata/if_expr/constant_condition.carbon
T
Jon Ross-Perkins 895e90e791 Start including the prelude for testing. (#3861)
- Adds an empty prelude.carbon file
- Imports that file in any non-Core package file
  - Adds --disable-prelude-import to avoid that
- Adds --exclude-dump-file-prefix to be able to hide files from dumping
- Used to hide core files (we can't do this by package name due to lex
dumps, for example)
- Restructures some tests to not rely on `i32`, particularly `alias`
tests (which rely on a name ref) and tests with no prelude.

I'm adding the framework for switching i32 to calling Int32 in the
prelude, but I'm running into a separate error actually switching over.
So that *mostly* works, but isn't quite ready for prime time. However,
maybe the current state of this PR is still useful to review since it
does a lot of the infrastructure work and adds the %Core everywhere?
2024-04-07 17:11:42 +00:00

218 lines
9.3 KiB
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
fn A() -> i32 { return 1; }
fn B() -> i32 { return 2; }
fn F() -> i32 {
return if true then A() else B();
}
fn G() -> i32 {
return if false then A() else B();
}
fn Constant() -> i32 {
var v: if true then i32 else i32* = 1;
var w: if false then i32 else i32* = &v;
return *w;
}
fn PartiallyConstant(t: type) -> i32 {
var v: if true then i32 else t = 1;
var w: if false then t else i32* = &v;
return *w;
}
// CHECK:STDOUT: --- constant_condition.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: %.1: i32 = int_literal 1 [template]
// CHECK:STDOUT: %.2: i32 = int_literal 2 [template]
// CHECK:STDOUT: %.3: bool = bool_literal true [template]
// CHECK:STDOUT: %.4: bool = bool_literal false [template]
// CHECK:STDOUT: %.5: type = ptr_type i32 [template]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: package: <namespace> = namespace [template] {
// CHECK:STDOUT: .Core = %Core
// CHECK:STDOUT: .A = %A
// CHECK:STDOUT: .B = %B
// CHECK:STDOUT: .F = %F
// CHECK:STDOUT: .G = %G
// CHECK:STDOUT: .Constant = %Constant
// CHECK:STDOUT: .PartiallyConstant = %PartiallyConstant
// CHECK:STDOUT: }
// CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
// CHECK:STDOUT: %A: <function> = fn_decl @A [template] {
// CHECK:STDOUT: %return.var.loc7: ref i32 = var <return slot>
// CHECK:STDOUT: }
// CHECK:STDOUT: %B: <function> = fn_decl @B [template] {
// CHECK:STDOUT: %return.var.loc8: ref i32 = var <return slot>
// CHECK:STDOUT: }
// CHECK:STDOUT: %F: <function> = fn_decl @F [template] {
// CHECK:STDOUT: %return.var.loc10: ref i32 = var <return slot>
// CHECK:STDOUT: }
// CHECK:STDOUT: %G: <function> = fn_decl @G [template] {
// CHECK:STDOUT: %return.var.loc14: ref i32 = var <return slot>
// CHECK:STDOUT: }
// CHECK:STDOUT: %Constant: <function> = fn_decl @Constant [template] {
// CHECK:STDOUT: %return.var.loc18: ref i32 = var <return slot>
// CHECK:STDOUT: }
// CHECK:STDOUT: %PartiallyConstant: <function> = fn_decl @PartiallyConstant [template] {
// CHECK:STDOUT: %t.loc24_22.1: type = param t
// CHECK:STDOUT: @PartiallyConstant.%t: type = bind_name t, %t.loc24_22.1
// CHECK:STDOUT: %return.var.loc24: ref i32 = var <return slot>
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @A() -> i32 {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %.loc7: i32 = int_literal 1 [template = constants.%.1]
// CHECK:STDOUT: return %.loc7
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @B() -> i32 {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %.loc8: i32 = int_literal 2 [template = constants.%.2]
// CHECK:STDOUT: return %.loc8
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @F() -> i32 {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %.loc11_13: bool = bool_literal true [template = constants.%.3]
// CHECK:STDOUT: if %.loc11_13 br !if.expr.then else br !if.expr.else
// CHECK:STDOUT:
// CHECK:STDOUT: !if.expr.then:
// CHECK:STDOUT: %A.ref: <function> = name_ref A, file.%A [template = file.%A]
// CHECK:STDOUT: %.loc11_24.1: init i32 = call %A.ref()
// CHECK:STDOUT: %.loc11_24.2: i32 = value_of_initializer %.loc11_24.1
// CHECK:STDOUT: %.loc11_24.3: i32 = converted %.loc11_24.1, %.loc11_24.2
// CHECK:STDOUT: br !if.expr.result(%.loc11_24.3)
// CHECK:STDOUT:
// CHECK:STDOUT: !if.expr.else:
// CHECK:STDOUT: %B.ref: <function> = name_ref B, file.%B [template = file.%B]
// CHECK:STDOUT: %.loc11_33.1: init i32 = call %B.ref()
// CHECK:STDOUT: %.loc11_27: i32 = value_of_initializer %.loc11_33.1
// CHECK:STDOUT: %.loc11_33.2: i32 = converted %.loc11_33.1, %.loc11_27
// CHECK:STDOUT: br !if.expr.result(%.loc11_33.2)
// CHECK:STDOUT:
// CHECK:STDOUT: !if.expr.result:
// CHECK:STDOUT: %.loc11_10: i32 = block_arg !if.expr.result
// CHECK:STDOUT: return %.loc11_10
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @G() -> i32 {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %.loc15_13: bool = bool_literal false [template = constants.%.4]
// CHECK:STDOUT: if %.loc15_13 br !if.expr.then else br !if.expr.else
// CHECK:STDOUT:
// CHECK:STDOUT: !if.expr.then:
// CHECK:STDOUT: %A.ref: <function> = name_ref A, file.%A [template = file.%A]
// CHECK:STDOUT: %.loc15_25.1: init i32 = call %A.ref()
// CHECK:STDOUT: %.loc15_25.2: i32 = value_of_initializer %.loc15_25.1
// CHECK:STDOUT: %.loc15_25.3: i32 = converted %.loc15_25.1, %.loc15_25.2
// CHECK:STDOUT: br !if.expr.result(%.loc15_25.3)
// CHECK:STDOUT:
// CHECK:STDOUT: !if.expr.else:
// CHECK:STDOUT: %B.ref: <function> = name_ref B, file.%B [template = file.%B]
// CHECK:STDOUT: %.loc15_34.1: init i32 = call %B.ref()
// CHECK:STDOUT: %.loc15_28: i32 = value_of_initializer %.loc15_34.1
// CHECK:STDOUT: %.loc15_34.2: i32 = converted %.loc15_34.1, %.loc15_28
// CHECK:STDOUT: br !if.expr.result(%.loc15_34.2)
// CHECK:STDOUT:
// CHECK:STDOUT: !if.expr.result:
// CHECK:STDOUT: %.loc15_10: i32 = block_arg !if.expr.result
// CHECK:STDOUT: return %.loc15_10
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @Constant() -> i32 {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %.loc19_13: bool = bool_literal true [template = constants.%.3]
// CHECK:STDOUT: if %.loc19_13 br !if.expr.then.loc19 else br !if.expr.else.loc19
// CHECK:STDOUT:
// CHECK:STDOUT: !if.expr.then.loc19:
// CHECK:STDOUT: br !if.expr.result.loc19(i32)
// CHECK:STDOUT:
// CHECK:STDOUT: !if.expr.else.loc19:
// CHECK:STDOUT: %.loc19_35: type = ptr_type i32 [template = constants.%.5]
// CHECK:STDOUT: br !if.expr.result.loc19(%.loc19_35)
// CHECK:STDOUT:
// CHECK:STDOUT: !if.expr.result.loc19:
// CHECK:STDOUT: %.loc19_10: type = block_arg !if.expr.result.loc19 [template = i32]
// CHECK:STDOUT: %v.var: ref i32 = var v
// CHECK:STDOUT: %v: ref i32 = bind_name v, %v.var
// CHECK:STDOUT: %.loc19_39: i32 = int_literal 1 [template = constants.%.1]
// CHECK:STDOUT: assign %v.var, %.loc19_39
// CHECK:STDOUT: %.loc20_13: bool = bool_literal false [template = constants.%.4]
// CHECK:STDOUT: if %.loc20_13 br !if.expr.then.loc20 else br !if.expr.else.loc20
// CHECK:STDOUT:
// CHECK:STDOUT: !if.expr.then.loc20:
// CHECK:STDOUT: br !if.expr.result.loc20(i32)
// CHECK:STDOUT:
// CHECK:STDOUT: !if.expr.else.loc20:
// CHECK:STDOUT: %.loc20_36: type = ptr_type i32 [template = constants.%.5]
// CHECK:STDOUT: br !if.expr.result.loc20(%.loc20_36)
// CHECK:STDOUT:
// CHECK:STDOUT: !if.expr.result.loc20:
// CHECK:STDOUT: %.loc20_10: type = block_arg !if.expr.result.loc20 [template = constants.%.5]
// CHECK:STDOUT: %w.var: ref i32* = var w
// CHECK:STDOUT: %w: ref i32* = bind_name w, %w.var
// CHECK:STDOUT: %v.ref: ref i32 = name_ref v, %v
// CHECK:STDOUT: %.loc20_40: i32* = addr_of %v.ref
// CHECK:STDOUT: assign %w.var, %.loc20_40
// CHECK:STDOUT: %w.ref: ref i32* = name_ref w, %w
// CHECK:STDOUT: %.loc21_11: i32* = bind_value %w.ref
// CHECK:STDOUT: %.loc21_10.1: ref i32 = deref %.loc21_11
// CHECK:STDOUT: %.loc21_10.2: i32 = bind_value %.loc21_10.1
// CHECK:STDOUT: return %.loc21_10.2
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @PartiallyConstant(%t: type) -> i32 {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %.loc25_13: bool = bool_literal true [template = constants.%.3]
// CHECK:STDOUT: if %.loc25_13 br !if.expr.then.loc25 else br !if.expr.else.loc25
// CHECK:STDOUT:
// CHECK:STDOUT: !if.expr.then.loc25:
// CHECK:STDOUT: br !if.expr.result.loc25(i32)
// CHECK:STDOUT:
// CHECK:STDOUT: !if.expr.else.loc25:
// CHECK:STDOUT: %t.ref.loc25: type = name_ref t, %t
// CHECK:STDOUT: br !if.expr.result.loc25(%t.ref.loc25)
// CHECK:STDOUT:
// CHECK:STDOUT: !if.expr.result.loc25:
// CHECK:STDOUT: %.loc25_10: type = block_arg !if.expr.result.loc25 [template = i32]
// CHECK:STDOUT: %v.var: ref i32 = var v
// CHECK:STDOUT: %v: ref i32 = bind_name v, %v.var
// CHECK:STDOUT: %.loc25_36: i32 = int_literal 1 [template = constants.%.1]
// CHECK:STDOUT: assign %v.var, %.loc25_36
// CHECK:STDOUT: %.loc26_13: bool = bool_literal false [template = constants.%.4]
// CHECK:STDOUT: if %.loc26_13 br !if.expr.then.loc26 else br !if.expr.else.loc26
// CHECK:STDOUT:
// CHECK:STDOUT: !if.expr.then.loc26:
// CHECK:STDOUT: %t.ref.loc26: type = name_ref t, %t
// CHECK:STDOUT: br !if.expr.result.loc26(%t.ref.loc26)
// CHECK:STDOUT:
// CHECK:STDOUT: !if.expr.else.loc26:
// CHECK:STDOUT: %.loc26_34: type = ptr_type i32 [template = constants.%.5]
// CHECK:STDOUT: br !if.expr.result.loc26(%.loc26_34)
// CHECK:STDOUT:
// CHECK:STDOUT: !if.expr.result.loc26:
// CHECK:STDOUT: %.loc26_10: type = block_arg !if.expr.result.loc26 [template = constants.%.5]
// CHECK:STDOUT: %w.var: ref i32* = var w
// CHECK:STDOUT: %w: ref i32* = bind_name w, %w.var
// CHECK:STDOUT: %v.ref: ref i32 = name_ref v, %v
// CHECK:STDOUT: %.loc26_38: i32* = addr_of %v.ref
// CHECK:STDOUT: assign %w.var, %.loc26_38
// CHECK:STDOUT: %w.ref: ref i32* = name_ref w, %w
// CHECK:STDOUT: %.loc27_11: i32* = bind_value %w.ref
// CHECK:STDOUT: %.loc27_10.1: ref i32 = deref %.loc27_11
// CHECK:STDOUT: %.loc27_10.2: i32 = bind_value %.loc27_10.1
// CHECK:STDOUT: return %.loc27_10.2
// CHECK:STDOUT: }
// CHECK:STDOUT: