Files
carbon-lang/core/prelude/operators/bitwise.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

98 lines
2.2 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
package Core library "prelude/operators/bitwise";
import library "prelude/types/int_literal";
// Bit complement: `^a`.
interface BitComplement {
fn Op[self: Self]() -> Self;
}
// Bitwise AND: `a & b`.
interface BitAnd {
fn Op[self: Self](other: Self) -> Self;
}
// Bitwise AND with assignment: `a &= b`.
interface BitAndAssign {
fn Op[addr self: Self*](other: Self);
}
// Bitwise OR: `a | b`.
interface BitOr {
fn Op[self: Self](other: Self) -> Self;
}
// Bitwise OR with assignment: `a |= b`.
interface BitOrAssign {
fn Op[addr self: Self*](other: Self);
}
// Bitwise XOR: `a ^ b`.
interface BitXor {
fn Op[self: Self](other: Self) -> Self;
}
// Bitwise XOR with assignment: `a ^= b`.
interface BitXorAssign {
fn Op[addr self: Self*](other: Self);
}
// Left shift: `a << b`.
interface LeftShift {
fn Op[self: Self](other: Self) -> Self;
}
// Left shift with assignment: `a <<= b`.
interface LeftShiftAssign {
fn Op[addr self: Self*](other: Self);
}
// Right shift: `a >> b`.
interface RightShift {
fn Op[self: Self](other: Self) -> Self;
}
// Right shift with assignment: `a >>= b`.
interface RightShiftAssign {
fn Op[addr self: Self*](other: Self);
}
// Operations for IntLiteral. These need to be here because IntLiteral has no
// associated library of its own.
impl IntLiteral() as BitAnd {
fn Op[self: Self](other: Self) -> Self = "int.and";
}
impl IntLiteral() as BitComplement {
fn Op[self: Self]() -> Self = "int.complement";
}
impl IntLiteral() as BitOr {
fn Op[self: Self](other: Self) -> Self = "int.or";
}
impl IntLiteral() as BitXor {
fn Op[self: Self](other: Self) -> Self = "int.xor";
}
impl IntLiteral() as LeftShift {
fn Op[self: Self](other: Self) -> Self = "int.left_shift";
}
impl IntLiteral() as RightShift {
fn Op[self: Self](other: Self) -> Self = "int.right_shift";
}
// Operations for `type`. These need to be here because `type` has no
// associated library of its own.
// Facet type combination.
impl type as BitAnd {
fn Op[self: Self](other: Self) -> Self = "type.and";
}