Files
carbon-lang/toolchain/check/testdata/builtins/float/negate.carbon
T
Jon Ross-PerkinsandRichard Smith 76ed3c73cb Promote FunctionType to a standard instruction. (#3931)
This removes the builtin FunctionType, replacing it with a FunctionType
instruction. The constant for a FunctionDecl is now a StructValue with
type of FunctionType.

Note this means a function declaration produces _both_ a type, and a
value of the type. This has some consequences in terms of circularity,
and makes the importing of function declarations a little more complex.

It'll get particularly peculiar for imports because of the behavior of
the reference, but that's a known issue due to other things such as
`alias`. The impact will hopefully be contained to
ResolvePrevInstForMerge (and ImportRefs).

To note a small formatting change in diagnostics:

```
-  // CHECK:STDERR: fail_member_lookup.carbon:[[@LINE+4]]:3: ERROR: Value of type `<associated <function> in Interface>` is not callable.
+  // CHECK:STDERR: fail_member_lookup.carbon:[[@LINE+4]]:3: ERROR: Value of type `<associated F in Interface>` is not callable.

-  // CHECK:STDERR: fail_todo_facet_lookup.carbon:[[@LINE+4]]:3: ERROR: Value of type `<associated <function> in Interface>` is not callable.
+  // CHECK:STDERR: fail_todo_facet_lookup.carbon:[[@LINE+4]]:3: ERROR: Value of type `<associated F in Interface>` is not callable.
```

---------

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2024-05-03 15:18:04 +00:00

237 lines
12 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
// --- float_negate.carbon
fn Negate(a: f64) -> f64 = "float.negate";
fn RuntimeCall(a: f64, b: f64) -> f64 {
return Negate(a);
}
let a: f64 = Negate(1.5);
// --- fail_bad_decl.carbon
package FailBadDecl api;
// CHECK:STDERR: fail_bad_decl.carbon:[[@LINE+4]]:1: ERROR: Invalid signature for builtin function "float.negate".
// CHECK:STDERR: fn TooFew() -> f64 = "float.negate";
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
fn TooFew() -> f64 = "float.negate";
// CHECK:STDERR: fail_bad_decl.carbon:[[@LINE+4]]:1: ERROR: Invalid signature for builtin function "float.negate".
// CHECK:STDERR: fn TooMany(a: f64, b: f64) -> f64 = "float.negate";
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
fn TooMany(a: f64, b: f64) -> f64 = "float.negate";
// CHECK:STDERR: fail_bad_decl.carbon:[[@LINE+4]]:1: ERROR: Invalid signature for builtin function "float.negate".
// CHECK:STDERR: fn BadReturnType(a: f64) -> bool = "float.negate";
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
fn BadReturnType(a: f64) -> bool = "float.negate";
fn JustRight(a: f64) -> f64 = "float.negate";
fn RuntimeCallTooFew(a: f64) -> f64 {
// CHECK:STDERR: fail_bad_decl.carbon:[[@LINE+7]]:10: ERROR: 1 argument(s) passed to function expecting 0 argument(s).
// CHECK:STDERR: return TooFew(a);
// CHECK:STDERR: ^~~~~~~
// CHECK:STDERR: fail_bad_decl.carbon:[[@LINE-17]]:1: Calling function declared here.
// CHECK:STDERR: fn TooFew() -> f64 = "float.negate";
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
return TooFew(a);
}
fn RuntimeCallTooMany(a: f64, b: f64, c: f64) -> f64 {
// CHECK:STDERR: fail_bad_decl.carbon:[[@LINE+7]]:10: ERROR: 3 argument(s) passed to function expecting 2 argument(s).
// CHECK:STDERR: return TooMany(a, b, c);
// CHECK:STDERR: ^~~~~~~~
// CHECK:STDERR: fail_bad_decl.carbon:[[@LINE-23]]:1: Calling function declared here.
// CHECK:STDERR: fn TooMany(a: f64, b: f64) -> f64 = "float.negate";
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
return TooMany(a, b, c);
}
fn RuntimeCallBadReturnType(a: f64, b: f64) -> bool {
// CHECK:STDERR: fail_bad_decl.carbon:[[@LINE+6]]:10: ERROR: 2 argument(s) passed to function expecting 1 argument(s).
// CHECK:STDERR: return BadReturnType(a, b);
// CHECK:STDERR: ^~~~~~~~~~~~~~
// CHECK:STDERR: fail_bad_decl.carbon:[[@LINE-29]]:1: Calling function declared here.
// CHECK:STDERR: fn BadReturnType(a: f64) -> bool = "float.negate";
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
return BadReturnType(a, b);
}
// CHECK:STDOUT: --- float_negate.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: %Negate: type = fn_type @Negate [template]
// CHECK:STDOUT: %.1: type = tuple_type () [template]
// CHECK:STDOUT: %struct.1: Negate = struct_value () [template]
// CHECK:STDOUT: %RuntimeCall: type = fn_type @RuntimeCall [template]
// CHECK:STDOUT: %struct.2: RuntimeCall = struct_value () [template]
// CHECK:STDOUT: %.2: f64 = float_literal 1.5 [template]
// CHECK:STDOUT: %.3: f64 = float_literal -1.5 [template]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: package: <namespace> = namespace [template] {
// CHECK:STDOUT: .Core = %Core
// CHECK:STDOUT: .Negate = %Negate.decl
// CHECK:STDOUT: .RuntimeCall = %RuntimeCall.decl
// CHECK:STDOUT: }
// CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
// CHECK:STDOUT: %Negate.decl: Negate = fn_decl @Negate [template = constants.%struct.1] {
// CHECK:STDOUT: %a.loc2_11.1: f64 = param a
// CHECK:STDOUT: @Negate.%a: f64 = bind_name a, %a.loc2_11.1
// CHECK:STDOUT: @Negate.%return: ref f64 = var <return slot>
// CHECK:STDOUT: }
// CHECK:STDOUT: %RuntimeCall.decl: RuntimeCall = fn_decl @RuntimeCall [template = constants.%struct.2] {
// CHECK:STDOUT: %a.loc4_16.1: f64 = param a
// CHECK:STDOUT: @RuntimeCall.%a: f64 = bind_name a, %a.loc4_16.1
// CHECK:STDOUT: %b.loc4_24.1: f64 = param b
// CHECK:STDOUT: @RuntimeCall.%b: f64 = bind_name b, %b.loc4_24.1
// CHECK:STDOUT: @RuntimeCall.%return: ref f64 = var <return slot>
// CHECK:STDOUT: }
// CHECK:STDOUT: %Negate.ref: Negate = name_ref Negate, %Negate.decl [template = constants.%struct.1]
// CHECK:STDOUT: %.loc8_21: f64 = float_literal 1.5 [template = constants.%.2]
// CHECK:STDOUT: %float.negate: init f64 = call %Negate.ref(%.loc8_21) [template = constants.%.3]
// CHECK:STDOUT: %.loc8_25.1: f64 = value_of_initializer %float.negate [template = constants.%.3]
// CHECK:STDOUT: %.loc8_25.2: f64 = converted %float.negate, %.loc8_25.1 [template = constants.%.3]
// CHECK:STDOUT: %a.loc8: f64 = bind_name a, %.loc8_25.2
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @Negate(%a: f64) -> f64 = "float.negate";
// CHECK:STDOUT:
// CHECK:STDOUT: fn @RuntimeCall(%a: f64, %b: f64) -> f64 {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %Negate.ref: Negate = name_ref Negate, file.%Negate.decl [template = constants.%struct.1]
// CHECK:STDOUT: %a.ref: f64 = name_ref a, %a
// CHECK:STDOUT: %float.negate: init f64 = call %Negate.ref(%a.ref)
// CHECK:STDOUT: %.loc5_19.1: f64 = value_of_initializer %float.negate
// CHECK:STDOUT: %.loc5_19.2: f64 = converted %float.negate, %.loc5_19.1
// CHECK:STDOUT: return %.loc5_19.2
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: --- fail_bad_decl.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: %TooFew: type = fn_type @TooFew [template]
// CHECK:STDOUT: %.1: type = tuple_type () [template]
// CHECK:STDOUT: %struct.1: TooFew = struct_value () [template]
// CHECK:STDOUT: %TooMany: type = fn_type @TooMany [template]
// CHECK:STDOUT: %struct.2: TooMany = struct_value () [template]
// CHECK:STDOUT: %BadReturnType: type = fn_type @BadReturnType [template]
// CHECK:STDOUT: %struct.3: BadReturnType = struct_value () [template]
// CHECK:STDOUT: %JustRight: type = fn_type @JustRight [template]
// CHECK:STDOUT: %struct.4: JustRight = struct_value () [template]
// CHECK:STDOUT: %RuntimeCallTooFew: type = fn_type @RuntimeCallTooFew [template]
// CHECK:STDOUT: %struct.5: RuntimeCallTooFew = struct_value () [template]
// CHECK:STDOUT: %RuntimeCallTooMany: type = fn_type @RuntimeCallTooMany [template]
// CHECK:STDOUT: %struct.6: RuntimeCallTooMany = struct_value () [template]
// CHECK:STDOUT: %RuntimeCallBadReturnType: type = fn_type @RuntimeCallBadReturnType [template]
// CHECK:STDOUT: %struct.7: RuntimeCallBadReturnType = struct_value () [template]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: package: <namespace> = namespace [template] {
// CHECK:STDOUT: .Core = %Core
// CHECK:STDOUT: .TooFew = %TooFew.decl
// CHECK:STDOUT: .TooMany = %TooMany.decl
// CHECK:STDOUT: .BadReturnType = %BadReturnType.decl
// CHECK:STDOUT: .JustRight = %JustRight.decl
// CHECK:STDOUT: .RuntimeCallTooFew = %RuntimeCallTooFew.decl
// CHECK:STDOUT: .RuntimeCallTooMany = %RuntimeCallTooMany.decl
// CHECK:STDOUT: .RuntimeCallBadReturnType = %RuntimeCallBadReturnType.decl
// CHECK:STDOUT: }
// CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
// CHECK:STDOUT: %TooFew.decl: TooFew = fn_decl @TooFew [template = constants.%struct.1] {
// CHECK:STDOUT: @TooFew.%return: ref f64 = var <return slot>
// CHECK:STDOUT: }
// CHECK:STDOUT: %TooMany.decl: TooMany = fn_decl @TooMany [template = constants.%struct.2] {
// CHECK:STDOUT: %a.loc13_12.1: f64 = param a
// CHECK:STDOUT: @TooMany.%a: f64 = bind_name a, %a.loc13_12.1
// CHECK:STDOUT: %b.loc13_20.1: f64 = param b
// CHECK:STDOUT: @TooMany.%b: f64 = bind_name b, %b.loc13_20.1
// CHECK:STDOUT: @TooMany.%return: ref f64 = var <return slot>
// CHECK:STDOUT: }
// CHECK:STDOUT: %BadReturnType.decl: BadReturnType = fn_decl @BadReturnType [template = constants.%struct.3] {
// CHECK:STDOUT: %a.loc18_18.1: f64 = param a
// CHECK:STDOUT: @BadReturnType.%a: f64 = bind_name a, %a.loc18_18.1
// CHECK:STDOUT: @BadReturnType.%return: ref bool = var <return slot>
// CHECK:STDOUT: }
// CHECK:STDOUT: %JustRight.decl: JustRight = fn_decl @JustRight [template = constants.%struct.4] {
// CHECK:STDOUT: %a.loc19_14.1: f64 = param a
// CHECK:STDOUT: @JustRight.%a: f64 = bind_name a, %a.loc19_14.1
// CHECK:STDOUT: @JustRight.%return: ref f64 = var <return slot>
// CHECK:STDOUT: }
// CHECK:STDOUT: %RuntimeCallTooFew.decl: RuntimeCallTooFew = fn_decl @RuntimeCallTooFew [template = constants.%struct.5] {
// CHECK:STDOUT: %a.loc21_22.1: f64 = param a
// CHECK:STDOUT: @RuntimeCallTooFew.%a: f64 = bind_name a, %a.loc21_22.1
// CHECK:STDOUT: @RuntimeCallTooFew.%return: ref f64 = var <return slot>
// CHECK:STDOUT: }
// CHECK:STDOUT: %RuntimeCallTooMany.decl: RuntimeCallTooMany = fn_decl @RuntimeCallTooMany [template = constants.%struct.6] {
// CHECK:STDOUT: %a.loc32_23.1: f64 = param a
// CHECK:STDOUT: @RuntimeCallTooMany.%a: f64 = bind_name a, %a.loc32_23.1
// CHECK:STDOUT: %b.loc32_31.1: f64 = param b
// CHECK:STDOUT: @RuntimeCallTooMany.%b: f64 = bind_name b, %b.loc32_31.1
// CHECK:STDOUT: %c.loc32_39.1: f64 = param c
// CHECK:STDOUT: @RuntimeCallTooMany.%c: f64 = bind_name c, %c.loc32_39.1
// CHECK:STDOUT: @RuntimeCallTooMany.%return: ref f64 = var <return slot>
// CHECK:STDOUT: }
// CHECK:STDOUT: %RuntimeCallBadReturnType.decl: RuntimeCallBadReturnType = fn_decl @RuntimeCallBadReturnType [template = constants.%struct.7] {
// CHECK:STDOUT: %a.loc43_29.1: f64 = param a
// CHECK:STDOUT: @RuntimeCallBadReturnType.%a: f64 = bind_name a, %a.loc43_29.1
// CHECK:STDOUT: %b.loc43_37.1: f64 = param b
// CHECK:STDOUT: @RuntimeCallBadReturnType.%b: f64 = bind_name b, %b.loc43_37.1
// CHECK:STDOUT: @RuntimeCallBadReturnType.%return: ref bool = var <return slot>
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @TooFew() -> f64;
// CHECK:STDOUT:
// CHECK:STDOUT: fn @TooMany(%a: f64, %b: f64) -> f64;
// CHECK:STDOUT:
// CHECK:STDOUT: fn @BadReturnType(%a: f64) -> bool;
// CHECK:STDOUT:
// CHECK:STDOUT: fn @JustRight(%a: f64) -> f64 = "float.negate";
// CHECK:STDOUT:
// CHECK:STDOUT: fn @RuntimeCallTooFew(%a: f64) -> f64 {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %TooFew.ref: TooFew = name_ref TooFew, file.%TooFew.decl [template = constants.%struct.1]
// CHECK:STDOUT: %a.ref: f64 = name_ref a, %a
// CHECK:STDOUT: %TooFew.call: init f64 = call %TooFew.ref(<invalid>) [template = <error>]
// CHECK:STDOUT: %.loc29_19.1: f64 = value_of_initializer %TooFew.call [template = <error>]
// CHECK:STDOUT: %.loc29_19.2: f64 = converted %TooFew.call, %.loc29_19.1 [template = <error>]
// CHECK:STDOUT: return %.loc29_19.2
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @RuntimeCallTooMany(%a: f64, %b: f64, %c: f64) -> f64 {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %TooMany.ref: TooMany = name_ref TooMany, file.%TooMany.decl [template = constants.%struct.2]
// CHECK:STDOUT: %a.ref: f64 = name_ref a, %a
// CHECK:STDOUT: %b.ref: f64 = name_ref b, %b
// CHECK:STDOUT: %c.ref: f64 = name_ref c, %c
// CHECK:STDOUT: %TooMany.call: init f64 = call %TooMany.ref(<invalid>) [template = <error>]
// CHECK:STDOUT: %.loc40_26.1: f64 = value_of_initializer %TooMany.call [template = <error>]
// CHECK:STDOUT: %.loc40_26.2: f64 = converted %TooMany.call, %.loc40_26.1 [template = <error>]
// CHECK:STDOUT: return %.loc40_26.2
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @RuntimeCallBadReturnType(%a: f64, %b: f64) -> bool {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %BadReturnType.ref: BadReturnType = name_ref BadReturnType, file.%BadReturnType.decl [template = constants.%struct.3]
// CHECK:STDOUT: %a.ref: f64 = name_ref a, %a
// CHECK:STDOUT: %b.ref: f64 = name_ref b, %b
// CHECK:STDOUT: %BadReturnType.call: init bool = call %BadReturnType.ref(<invalid>) [template = <error>]
// CHECK:STDOUT: %.loc50_29.1: bool = value_of_initializer %BadReturnType.call [template = <error>]
// CHECK:STDOUT: %.loc50_29.2: bool = converted %BadReturnType.call, %.loc50_29.1 [template = <error>]
// CHECK:STDOUT: return %.loc50_29.2
// CHECK:STDOUT: }
// CHECK:STDOUT: