fail_scope.carbon 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. //
  5. // AUTOUPDATE
  6. // TIP: To test this file alone, run:
  7. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/if/fail_scope.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/if/fail_scope.carbon
  10. fn VarScope(b: bool) -> i32 {
  11. if (b) {
  12. var n: i32 = 2;
  13. return n;
  14. }
  15. // CHECK:STDERR: fail_scope.carbon:[[@LINE+3]]:10: error: name `n` not found [NameNotFound]
  16. // CHECK:STDERR: return n;
  17. // CHECK:STDERR: ^
  18. return n;
  19. }
  20. // CHECK:STDOUT: --- fail_scope.carbon
  21. // CHECK:STDOUT:
  22. // CHECK:STDOUT: constants {
  23. // CHECK:STDOUT: %Bool.type: type = fn_type @Bool [template]
  24. // CHECK:STDOUT: %Bool: %Bool.type = struct_value () [template]
  25. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template]
  26. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [template]
  27. // CHECK:STDOUT: %VarScope.type: type = fn_type @VarScope [template]
  28. // CHECK:STDOUT: %VarScope: %VarScope.type = struct_value () [template]
  29. // CHECK:STDOUT: %int_2.1: Core.IntLiteral = int_value 2 [template]
  30. // CHECK:STDOUT: %Convert.type.2: type = fn_type @Convert.1, @ImplicitAs(%i32) [template]
  31. // CHECK:STDOUT: %Convert.type.10: type = fn_type @Convert.2, @impl.1(%int_32) [template]
  32. // CHECK:STDOUT: %Convert.10: %Convert.type.10 = struct_value () [template]
  33. // CHECK:STDOUT: %interface.5: <witness> = interface_witness (%Convert.10) [template]
  34. // CHECK:STDOUT: %Convert.bound: <bound method> = bound_method %int_2.1, %Convert.10 [template]
  35. // CHECK:STDOUT: %Convert.specific_fn: <specific function> = specific_function %Convert.bound, @Convert.2(%int_32) [template]
  36. // CHECK:STDOUT: %int_2.2: %i32 = int_value 2 [template]
  37. // CHECK:STDOUT: }
  38. // CHECK:STDOUT:
  39. // CHECK:STDOUT: imports {
  40. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  41. // CHECK:STDOUT: .Bool = %import_ref.1
  42. // CHECK:STDOUT: .Int = %import_ref.2
  43. // CHECK:STDOUT: .ImplicitAs = %import_ref.6
  44. // CHECK:STDOUT: import Core//prelude
  45. // CHECK:STDOUT: import Core//prelude/...
  46. // CHECK:STDOUT: }
  47. // CHECK:STDOUT: }
  48. // CHECK:STDOUT:
  49. // CHECK:STDOUT: file {
  50. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  51. // CHECK:STDOUT: .Core = imports.%Core
  52. // CHECK:STDOUT: .VarScope = %VarScope.decl
  53. // CHECK:STDOUT: }
  54. // CHECK:STDOUT: %Core.import = import Core
  55. // CHECK:STDOUT: %VarScope.decl: %VarScope.type = fn_decl @VarScope [template = constants.%VarScope] {
  56. // CHECK:STDOUT: %b.patt: bool = binding_pattern b
  57. // CHECK:STDOUT: %b.param_patt: bool = value_param_pattern %b.patt, runtime_param0
  58. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  59. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, runtime_param1
  60. // CHECK:STDOUT: } {
  61. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [template = constants.%int_32]
  62. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [template = constants.%i32]
  63. // CHECK:STDOUT: %b.param: bool = value_param runtime_param0
  64. // CHECK:STDOUT: %.loc11_16.3: type = splice_block %.loc11_16.2 [template = bool] {
  65. // CHECK:STDOUT: %bool.make_type: init type = call constants.%Bool() [template = bool]
  66. // CHECK:STDOUT: %.loc11_16.1: type = value_of_initializer %bool.make_type [template = bool]
  67. // CHECK:STDOUT: %.loc11_16.2: type = converted %bool.make_type, %.loc11_16.1 [template = bool]
  68. // CHECK:STDOUT: }
  69. // CHECK:STDOUT: %b: bool = bind_name b, %b.param
  70. // CHECK:STDOUT: %return.param: ref %i32 = out_param runtime_param1
  71. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  72. // CHECK:STDOUT: }
  73. // CHECK:STDOUT: }
  74. // CHECK:STDOUT:
  75. // CHECK:STDOUT: fn @VarScope(%b.param_patt: bool) -> %i32 {
  76. // CHECK:STDOUT: !entry:
  77. // CHECK:STDOUT: %b.ref: bool = name_ref b, %b
  78. // CHECK:STDOUT: if %b.ref br !if.then else br !if.else
  79. // CHECK:STDOUT:
  80. // CHECK:STDOUT: !if.then:
  81. // CHECK:STDOUT: %n.var: ref %i32 = var n
  82. // CHECK:STDOUT: %n: ref %i32 = bind_name n, %n.var
  83. // CHECK:STDOUT: %int_2: Core.IntLiteral = int_value 2 [template = constants.%int_2.1]
  84. // CHECK:STDOUT: %impl.elem0: %Convert.type.2 = interface_witness_access constants.%interface.5, element0 [template = constants.%Convert.10]
  85. // CHECK:STDOUT: %Convert.bound: <bound method> = bound_method %int_2, %impl.elem0 [template = constants.%Convert.bound]
  86. // CHECK:STDOUT: %Convert.specific_fn: <specific function> = specific_function %Convert.bound, @Convert.2(constants.%int_32) [template = constants.%Convert.specific_fn]
  87. // CHECK:STDOUT: %int.convert_checked: init %i32 = call %Convert.specific_fn(%int_2) [template = constants.%int_2.2]
  88. // CHECK:STDOUT: %.loc13: init %i32 = converted %int_2, %int.convert_checked [template = constants.%int_2.2]
  89. // CHECK:STDOUT: assign %n.var, %.loc13
  90. // CHECK:STDOUT: %n.ref.loc14: ref %i32 = name_ref n, %n
  91. // CHECK:STDOUT: %.loc14: %i32 = bind_value %n.ref.loc14
  92. // CHECK:STDOUT: return %.loc14
  93. // CHECK:STDOUT:
  94. // CHECK:STDOUT: !if.else:
  95. // CHECK:STDOUT: %n.ref.loc19: <error> = name_ref n, <error> [template = <error>]
  96. // CHECK:STDOUT: return <error>
  97. // CHECK:STDOUT: }
  98. // CHECK:STDOUT: