Files
carbon-lang/toolchain/check/testdata/operators/builtin/bit_and.carbon
T
Dana Jansens 288181453e Prevent BitAnd for facet types from matching non-type values (#5111)
Currently this test fails with trying to access a comptime function with
runtime values:
```
fn F() {
  let a: J = {} as J;
  let b: J = {} as J;
  // CHECK:STDERR: fail_bit_and_values_no_impl.carbon:[[@LINE+7]]:3: error: non-constant call to compile-time-only function [NonConstantCallToCompTimeOnlyFunction]
  // CHECK:STDERR:   a & b;
  // CHECK:STDERR:   ^~~~~
  // CHECK:STDERR: core/prelude/operators/bitwise.carbon:96:3: note: compile-time-only function declared here [CompTimeOnlyFunctionHere]
  // CHECK:STDERR:   fn Op[self: Self](other: Self) -> Self = "type.and";
  // CHECK:STDERR:   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  // CHECK:STDERR:
  a & b;
}
```
The issue is that the BitAnd impl for facet types is matching on any
value of any type:
```
impl forall [T:! type] T as BitAnd
```
What we really want is for it to match on facet types (which are type
values), which is written as:
```
impl type as BitAnd
```
After this change, the error makes more sense in the above test:
```
fn F() {
  let a: J = {} as J;
  let b: J = {} as J;
  // CHECK:STDERR: fail_bit_and_values_no_impl.carbon:[[@LINE+4]]:3: error: cannot access member of interface `Core.BitAnd` in type `J` that does not implement that interface [MissingImplInMemberAccess]
  // CHECK:STDERR:   a & b;
  // CHECK:STDERR:   ^~~~~
  // CHECK:STDERR:
  a & b;
}
```
2025-03-12 18:35:03 +00:00

77 lines
2.5 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
//
// EXTRA-ARGS: --no-dump-sem-ir
//
// AUTOUPDATE
// TIP: To test this file alone, run:
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/operators/builtin/bit_and.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/operators/builtin/bit_and.carbon
// --- bit_and_facet_types.carbon
library "[[@TEST_NAME]]";
interface I {}
interface J {}
fn F1[T:! I & J](t: T) {}
fn F2[T:! I & I](t: T) {}
// --- fail_bit_and_non_facet_types.carbon
library "[[@TEST_NAME]]";
interface I {}
class J {}
class K {}
// CHECK:STDERR: fail_bit_and_non_facet_types.carbon:[[@LINE+4]]:11: error: non-facet type `J` combined with `&` operator [FacetTypeRequiredForTypeAndOperator]
// CHECK:STDERR: fn F1[T:! I & J](t: T) {}
// CHECK:STDERR: ^~~~~
// CHECK:STDERR:
fn F1[T:! I & J](t: T) {}
// CHECK:STDERR: fail_bit_and_non_facet_types.carbon:[[@LINE+4]]:11: error: non-facet type `J` combined with `&` operator [FacetTypeRequiredForTypeAndOperator]
// CHECK:STDERR: fn F2[T:! J & I](t: T) {}
// CHECK:STDERR: ^~~~~
// CHECK:STDERR:
fn F2[T:! J & I](t: T) {}
// CHECK:STDERR: fail_bit_and_non_facet_types.carbon:[[@LINE+8]]:11: error: non-facet type `J` combined with `&` operator [FacetTypeRequiredForTypeAndOperator]
// CHECK:STDERR: fn F3[T:! J & K](t: T) {}
// CHECK:STDERR: ^~~~~
// CHECK:STDERR:
// CHECK:STDERR: fail_bit_and_non_facet_types.carbon:[[@LINE+4]]:11: error: non-facet type `K` combined with `&` operator [FacetTypeRequiredForTypeAndOperator]
// CHECK:STDERR: fn F3[T:! J & K](t: T) {}
// CHECK:STDERR: ^~~~~
// CHECK:STDERR:
fn F3[T:! J & K](t: T) {}
// --- bit_and_values_with_impl.carbon
library "[[@TEST_NAME]]";
class J { adapt {}; }
impl J as Core.BitAnd {
fn Op[self: Self](other: Self) -> Self { return {} as J; }
}
fn F() {
let a: J = {} as J;
let b: J = {} as J;
a & b;
}
// --- fail_bit_and_values_no_impl.carbon
library "[[@TEST_NAME]]";
class J { adapt {}; }
fn F() {
let a: J = {} as J;
let b: J = {} as J;
// CHECK:STDERR: fail_bit_and_values_no_impl.carbon:[[@LINE+4]]:3: error: cannot access member of interface `Core.BitAnd` in type `J` that does not implement that interface [MissingImplInMemberAccess]
// CHECK:STDERR: a & b;
// CHECK:STDERR: ^~~~~
// CHECK:STDERR:
a & b;
}