Issue 203374
Summary Remove `friend` Declarations from HLSL
Labels HLSL
Assignees
Reporter llvm-beanz
    HLSL disallows all uses of the `friend` keyword. Clang must diagnose every form of `friend` declaration in HLSL shader code.

## HLSL Context
The `friend` keyword is tightly coupled with C++ access control (private/protected members), user-defined constructors, and class encapsulation — all features that are restricted or removed in HLSL. Since there is no meaningful access control in HLSL types, `friend` declarations have no defined semantics in the HLSL specification.

## Current Behavior in Clang
`friend` declarations in HLSL mode are currently accepted without any HLSL-specific diagnostic.

See example: https://godbolt.org/z/Yoz9dWTj3

## Forms That Must Be Diagnosed

The following are all legal C++ but must be errors in HLSL:

**Friend class declaration**
```cpp
struct Bar {};
struct Foo {
    friend Bar; // grants Bar access to Foo's private members
};
```

**Friend function declaration**
```cpp
struct Foo {
    friend void helper(Foo f); // declares a non-member friend function
};
```

**Inline friend function definition**
```cpp
struct Vec2 {
    float x, y;
    friend Vec2 operator+(Vec2 a, Vec2 b) { // defined inline inside the class
        return {a.x + b.x, a.y + b.y};
    }
};
```

**Friend template class**
```cpp
template <typename T>
struct Handle {
    template <typename U>
    friend struct Handle; // all specialisations of Handle are friends of each other
};
```

**Friend template function**
```cpp
template <typename T>
struct Wrapper {
    template <typename U>
    friend bool operator==(Wrapper<U> a, Wrapper<U> b);
};
```

**Friend declaration granting access to a private member**
```cpp
struct Secret {
private:
    int value;
    friend int reveal(Secret s); // reveal() can read Secret::value
};
```

## Required Changes in Clang

### Diagnostic
Use a shared `err_hlsl_unsupported_feature` diagnostic (parameterized by feature name) rather than adding a per-feature entry to `DiagnosticSemaKinds.td`.

### Sema Check
In `SemaDecl.cpp` or `SemaHLSL.cpp`, when any `FriendDecl` is processed in HLSL mode, emit the diagnostic:
```cpp
if (getLangOpts().HLSL)
  Diag(FriendLoc, diag::err_hlsl_unsupported_feature) << "friend declarations";
```

This single check covers all forms — friend class declarations, friend function declarations, inline friend function definitions, and friend templates.

### Test
Add a test to `clang/test/SemaHLSL/` verifying that each of the forms above produces the diagnostic.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to