Files
carbon-lang/toolchain/check/testdata/pointer/fail_address_of_value.carbon
T
Jon Ross-Perkins 55300d1ebb Switch type literals to use builtin functions. (#3978)
Modifies the core package and literal handling to use factory functions
for standard type literals.

Updates function/builtin/import.carbon to stop depending on the prelude,
since it would list all the impls in the core file. Updates
alias/builtins.carbon to be failing (the type values cannot be aliased).
2024-05-23 16:00:08 +00:00

287 lines
15 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 G() -> i32;
fn H() -> {.a: i32};
fn AddressOfLiteral() {
// CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: ERROR: Cannot take the address of non-reference expression.
// CHECK:STDERR: &0;
// CHECK:STDERR: ^
// CHECK:STDERR:
&0;
// CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: ERROR: Cannot take the address of non-reference expression.
// CHECK:STDERR: &true;
// CHECK:STDERR: ^
// CHECK:STDERR:
&true;
// CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: ERROR: Cannot take the address of non-reference expression.
// CHECK:STDERR: &1.0;
// CHECK:STDERR: ^
// CHECK:STDERR:
&1.0;
// CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: ERROR: Cannot take the address of non-reference expression.
// CHECK:STDERR: &"Hello";
// CHECK:STDERR: ^
// CHECK:STDERR:
&"Hello";
// CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: ERROR: Cannot take the address of non-reference expression.
// CHECK:STDERR: &(1, 2);
// CHECK:STDERR: ^
// CHECK:STDERR:
&(1, 2);
// CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: ERROR: Cannot take the address of non-reference expression.
// CHECK:STDERR: &{.a = 5};
// CHECK:STDERR: ^
// CHECK:STDERR:
&{.a = 5};
}
fn AddressOfOperator() {
// CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: ERROR: Cannot take the address of non-reference expression.
// CHECK:STDERR: &(true and false);
// CHECK:STDERR: ^
// CHECK:STDERR:
&(true and false);
// CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: ERROR: Cannot take the address of a temporary object.
// CHECK:STDERR: &H().a;
// CHECK:STDERR: ^
// CHECK:STDERR:
&H().a;
// CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: ERROR: Cannot take the address of non-reference expression.
// CHECK:STDERR: &(not true);
// CHECK:STDERR: ^
// CHECK:STDERR:
&(not true);
}
fn AddressOfCall() {
// CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: ERROR: Cannot take the address of non-reference expression.
// CHECK:STDERR: &G();
// CHECK:STDERR: ^
// CHECK:STDERR:
&G();
}
fn AddressOfType() {
// CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: ERROR: Cannot take the address of non-reference expression.
// CHECK:STDERR: &i32;
// CHECK:STDERR: ^
// CHECK:STDERR:
&i32;
// CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: ERROR: Cannot take the address of non-reference expression.
// CHECK:STDERR: &(const i32*);
// CHECK:STDERR: ^
// CHECK:STDERR:
&(const i32*);
}
fn AddressOfTupleElementValue() {
// CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: ERROR: Cannot take the address of non-reference expression.
// CHECK:STDERR: &((1, 2)[0]);
// CHECK:STDERR: ^
// CHECK:STDERR:
&((1, 2)[0]);
}
fn AddressOfParam(param: i32) {
// CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+3]]:26: ERROR: Cannot take the address of non-reference expression.
// CHECK:STDERR: var param_addr: i32* = &param;
// CHECK:STDERR: ^
var param_addr: i32* = &param;
}
// CHECK:STDOUT: --- fail_address_of_value.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: %Int32: type = fn_type @Int32 [template]
// CHECK:STDOUT: %.1: type = tuple_type () [template]
// CHECK:STDOUT: %struct.1: Int32 = struct_value () [template]
// CHECK:STDOUT: %G: type = fn_type @G [template]
// CHECK:STDOUT: %struct.2: G = struct_value () [template]
// CHECK:STDOUT: %.2: type = struct_type {.a: i32} [template]
// CHECK:STDOUT: %H: type = fn_type @H [template]
// CHECK:STDOUT: %struct.3: H = struct_value () [template]
// CHECK:STDOUT: %AddressOfLiteral: type = fn_type @AddressOfLiteral [template]
// CHECK:STDOUT: %struct.4: AddressOfLiteral = struct_value () [template]
// CHECK:STDOUT: %.3: i32 = int_literal 0 [template]
// CHECK:STDOUT: %.4: type = ptr_type i32 [template]
// CHECK:STDOUT: %.5: bool = bool_literal true [template]
// CHECK:STDOUT: %.6: type = ptr_type bool [template]
// CHECK:STDOUT: %.7: f64 = float_literal 1 [template]
// CHECK:STDOUT: %.8: type = ptr_type f64 [template]
// CHECK:STDOUT: %.9: type = ptr_type String [template]
// CHECK:STDOUT: %.10: String = string_literal "Hello" [template]
// CHECK:STDOUT: %.11: i32 = int_literal 1 [template]
// CHECK:STDOUT: %.12: i32 = int_literal 2 [template]
// CHECK:STDOUT: %.13: type = tuple_type (i32, i32) [template]
// CHECK:STDOUT: %.14: type = ptr_type (i32, i32) [template]
// CHECK:STDOUT: %.15: i32 = int_literal 5 [template]
// CHECK:STDOUT: %.16: type = ptr_type {.a: i32} [template]
// CHECK:STDOUT: %AddressOfOperator: type = fn_type @AddressOfOperator [template]
// CHECK:STDOUT: %struct.5: AddressOfOperator = struct_value () [template]
// CHECK:STDOUT: %.17: bool = bool_literal false [template]
// CHECK:STDOUT: %AddressOfCall: type = fn_type @AddressOfCall [template]
// CHECK:STDOUT: %struct.6: AddressOfCall = struct_value () [template]
// CHECK:STDOUT: %AddressOfType: type = fn_type @AddressOfType [template]
// CHECK:STDOUT: %struct.7: AddressOfType = struct_value () [template]
// CHECK:STDOUT: %.18: type = ptr_type type [template]
// CHECK:STDOUT: %.19: type = const_type i32 [template]
// CHECK:STDOUT: %.20: type = ptr_type const i32 [template]
// CHECK:STDOUT: %AddressOfTupleElementValue: type = fn_type @AddressOfTupleElementValue [template]
// CHECK:STDOUT: %struct.8: AddressOfTupleElementValue = struct_value () [template]
// CHECK:STDOUT: %tuple: (i32, i32) = tuple_value (%.11, %.12) [template]
// CHECK:STDOUT: %AddressOfParam: type = fn_type @AddressOfParam [template]
// CHECK:STDOUT: %struct.9: AddressOfParam = struct_value () [template]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: package: <namespace> = namespace [template] {
// CHECK:STDOUT: .Core = %Core
// CHECK:STDOUT: .G = %G.decl
// CHECK:STDOUT: .H = %H.decl
// CHECK:STDOUT: .AddressOfLiteral = %AddressOfLiteral.decl
// CHECK:STDOUT: .AddressOfOperator = %AddressOfOperator.decl
// CHECK:STDOUT: .AddressOfCall = %AddressOfCall.decl
// CHECK:STDOUT: .AddressOfType = %AddressOfType.decl
// CHECK:STDOUT: .AddressOfTupleElementValue = %AddressOfTupleElementValue.decl
// CHECK:STDOUT: .AddressOfParam = %AddressOfParam.decl
// CHECK:STDOUT: }
// CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
// CHECK:STDOUT: %import_ref.1: Int32 = import_ref ir3, inst+3, loaded [template = constants.%struct.1]
// CHECK:STDOUT: %G.decl: G = fn_decl @G [template = constants.%struct.2] {
// CHECK:STDOUT: %int.make_type_32.loc7: init type = call constants.%struct.1() [template = i32]
// CHECK:STDOUT: %.loc7_11.1: type = value_of_initializer %int.make_type_32.loc7 [template = i32]
// CHECK:STDOUT: %.loc7_11.2: type = converted %int.make_type_32.loc7, %.loc7_11.1 [template = i32]
// CHECK:STDOUT: @G.%return: ref i32 = var <return slot>
// CHECK:STDOUT: }
// CHECK:STDOUT: %import_ref.2: Int32 = import_ref ir3, inst+3, loaded [template = constants.%struct.1]
// CHECK:STDOUT: %H.decl: H = fn_decl @H [template = constants.%struct.3] {
// CHECK:STDOUT: %int.make_type_32.loc9: init type = call constants.%struct.1() [template = i32]
// CHECK:STDOUT: %.loc9_16.1: type = value_of_initializer %int.make_type_32.loc9 [template = i32]
// CHECK:STDOUT: %.loc9_16.2: type = converted %int.make_type_32.loc9, %.loc9_16.1 [template = i32]
// CHECK:STDOUT: %.loc9_19: type = struct_type {.a: i32} [template = constants.%.2]
// CHECK:STDOUT: @H.%return: ref {.a: i32} = var <return slot>
// CHECK:STDOUT: }
// CHECK:STDOUT: %AddressOfLiteral.decl: AddressOfLiteral = fn_decl @AddressOfLiteral [template = constants.%struct.4] {}
// CHECK:STDOUT: %AddressOfOperator.decl: AddressOfOperator = fn_decl @AddressOfOperator [template = constants.%struct.5] {}
// CHECK:STDOUT: %AddressOfCall.decl: AddressOfCall = fn_decl @AddressOfCall [template = constants.%struct.6] {}
// CHECK:STDOUT: %AddressOfType.decl: AddressOfType = fn_decl @AddressOfType [template = constants.%struct.7] {}
// CHECK:STDOUT: %import_ref.3: Int32 = import_ref ir3, inst+3, loaded [template = constants.%struct.1]
// CHECK:STDOUT: %import_ref.4: Int32 = import_ref ir3, inst+3, loaded [template = constants.%struct.1]
// CHECK:STDOUT: %AddressOfTupleElementValue.decl: AddressOfTupleElementValue = fn_decl @AddressOfTupleElementValue [template = constants.%struct.8] {}
// CHECK:STDOUT: %import_ref.5: Int32 = import_ref ir3, inst+3, loaded [template = constants.%struct.1]
// CHECK:STDOUT: %AddressOfParam.decl: AddressOfParam = fn_decl @AddressOfParam [template = constants.%struct.9] {
// CHECK:STDOUT: %int.make_type_32.loc91: init type = call constants.%struct.1() [template = i32]
// CHECK:STDOUT: %.loc91_26.1: type = value_of_initializer %int.make_type_32.loc91 [template = i32]
// CHECK:STDOUT: %.loc91_26.2: type = converted %int.make_type_32.loc91, %.loc91_26.1 [template = i32]
// CHECK:STDOUT: %param.loc91_19.1: i32 = param param
// CHECK:STDOUT: @AddressOfParam.%param: i32 = bind_name param, %param.loc91_19.1
// CHECK:STDOUT: }
// CHECK:STDOUT: %import_ref.6: Int32 = import_ref ir3, inst+3, loaded [template = constants.%struct.1]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
// CHECK:STDOUT:
// CHECK:STDOUT: fn @G() -> i32;
// CHECK:STDOUT:
// CHECK:STDOUT: fn @H() -> {.a: i32};
// CHECK:STDOUT:
// CHECK:STDOUT: fn @AddressOfLiteral() {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %.loc16_4: i32 = int_literal 0 [template = constants.%.3]
// CHECK:STDOUT: %.loc16_3: i32* = addr_of <error> [template = <error>]
// CHECK:STDOUT: %.loc21_4: bool = bool_literal true [template = constants.%.5]
// CHECK:STDOUT: %.loc21_3: bool* = addr_of <error> [template = <error>]
// CHECK:STDOUT: %.loc26_4: f64 = float_literal 1 [template = constants.%.7]
// CHECK:STDOUT: %.loc26_3: f64* = addr_of <error> [template = <error>]
// CHECK:STDOUT: %.loc31_4: String = string_literal "Hello" [template = constants.%.10]
// CHECK:STDOUT: %.loc31_3: String* = addr_of <error> [template = <error>]
// CHECK:STDOUT: %.loc36_5: i32 = int_literal 1 [template = constants.%.11]
// CHECK:STDOUT: %.loc36_8: i32 = int_literal 2 [template = constants.%.12]
// CHECK:STDOUT: %.loc36_9: (i32, i32) = tuple_literal (%.loc36_5, %.loc36_8)
// CHECK:STDOUT: %.loc36_3: (i32, i32)* = addr_of <error> [template = <error>]
// CHECK:STDOUT: %.loc41_10: i32 = int_literal 5 [template = constants.%.15]
// CHECK:STDOUT: %.loc41_11: {.a: i32} = struct_literal (%.loc41_10)
// CHECK:STDOUT: %.loc41_3: {.a: i32}* = addr_of <error> [template = <error>]
// CHECK:STDOUT: return
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @AddressOfOperator() {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %.loc49_5: bool = bool_literal true [template = constants.%.5]
// CHECK:STDOUT: %.loc49_10.1: bool = bool_literal false [template = constants.%.17]
// CHECK:STDOUT: if %.loc49_5 br !and.rhs else br !and.result(%.loc49_10.1)
// CHECK:STDOUT:
// CHECK:STDOUT: !and.rhs:
// CHECK:STDOUT: %.loc49_14: bool = bool_literal false [template = constants.%.17]
// CHECK:STDOUT: br !and.result(%.loc49_14)
// CHECK:STDOUT:
// CHECK:STDOUT: !and.result:
// CHECK:STDOUT: %.loc49_10.2: bool = block_arg !and.result [template = constants.%.17]
// CHECK:STDOUT: %.loc49_3: bool* = addr_of <error> [template = <error>]
// CHECK:STDOUT: %H.ref: H = name_ref H, file.%H.decl [template = constants.%struct.3]
// CHECK:STDOUT: %H.call: init {.a: i32} = call %H.ref()
// CHECK:STDOUT: %.loc54_5.1: ref {.a: i32} = temporary_storage
// CHECK:STDOUT: %.loc54_5.2: ref {.a: i32} = temporary %.loc54_5.1, %H.call
// CHECK:STDOUT: %.loc54_7: ref i32 = struct_access %.loc54_5.2, element0
// CHECK:STDOUT: %.loc54_3: i32* = addr_of <error> [template = <error>]
// CHECK:STDOUT: %.loc59_9: bool = bool_literal true [template = constants.%.5]
// CHECK:STDOUT: %.loc59_5: bool = not %.loc59_9 [template = constants.%.17]
// CHECK:STDOUT: %.loc59_3: bool* = addr_of <error> [template = <error>]
// CHECK:STDOUT: return
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @AddressOfCall() {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %G.ref: G = name_ref G, file.%G.decl [template = constants.%struct.2]
// CHECK:STDOUT: %G.call: init i32 = call %G.ref()
// CHECK:STDOUT: %.loc67: i32* = addr_of <error> [template = <error>]
// CHECK:STDOUT: return
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @AddressOfType() {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %int.make_type_32.loc75: init type = call constants.%struct.1() [template = i32]
// CHECK:STDOUT: %.loc75: type* = addr_of <error> [template = <error>]
// CHECK:STDOUT: %int.make_type_32.loc80: init type = call constants.%struct.1() [template = i32]
// CHECK:STDOUT: %.loc80_5.1: type = value_of_initializer %int.make_type_32.loc80 [template = i32]
// CHECK:STDOUT: %.loc80_5.2: type = converted %int.make_type_32.loc80, %.loc80_5.1 [template = i32]
// CHECK:STDOUT: %.loc80_5.3: type = const_type i32 [template = constants.%.19]
// CHECK:STDOUT: %.loc80_14: type = ptr_type const i32 [template = constants.%.20]
// CHECK:STDOUT: %.loc80_3: type* = addr_of <error> [template = <error>]
// CHECK:STDOUT: return
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @AddressOfTupleElementValue() {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %.loc88_6: i32 = int_literal 1 [template = constants.%.11]
// CHECK:STDOUT: %.loc88_9: i32 = int_literal 2 [template = constants.%.12]
// CHECK:STDOUT: %.loc88_10.1: (i32, i32) = tuple_literal (%.loc88_6, %.loc88_9)
// CHECK:STDOUT: %.loc88_12: i32 = int_literal 0 [template = constants.%.3]
// CHECK:STDOUT: %tuple: (i32, i32) = tuple_value (%.loc88_6, %.loc88_9) [template = constants.%tuple]
// CHECK:STDOUT: %.loc88_10.2: (i32, i32) = converted %.loc88_10.1, %tuple [template = constants.%tuple]
// CHECK:STDOUT: %.loc88_13: i32 = tuple_index %.loc88_10.2, %.loc88_12 [template = constants.%.11]
// CHECK:STDOUT: %.loc88_3: i32* = addr_of <error> [template = <error>]
// CHECK:STDOUT: return
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @AddressOfParam(%param: i32) {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %int.make_type_32: init type = call constants.%struct.1() [template = i32]
// CHECK:STDOUT: %.loc95_22.1: type = value_of_initializer %int.make_type_32 [template = i32]
// CHECK:STDOUT: %.loc95_22.2: type = converted %int.make_type_32, %.loc95_22.1 [template = i32]
// CHECK:STDOUT: %.loc95_22.3: type = ptr_type i32 [template = constants.%.4]
// CHECK:STDOUT: %param_addr.var: ref i32* = var param_addr
// CHECK:STDOUT: %param_addr: ref i32* = bind_name param_addr, %param_addr.var
// CHECK:STDOUT: %param.ref: i32 = name_ref param, %param
// CHECK:STDOUT: %.loc95_26: i32* = addr_of <error> [template = <error>]
// CHECK:STDOUT: assign %param_addr.var, %.loc95_26
// CHECK:STDOUT: return
// CHECK:STDOUT: }
// CHECK:STDOUT: