llvm / llvm/llvm-project

[Casting] `ConstStrippingForwardingCast` doesn't add `const` to the return type

Open
#217,574 0 comments 0 reactions 0 assignees View on GitHub
llvm:support
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Current implementation

```cpp
template
struct ConstStrippingForwardingCast {
// Remove the pointer if it exists, then we can get rid of consts/volatiles.
using DecayedFrom = std::remove_cv_t>;
// Now if it's a pointer, add it back. Otherwise, we want a ref.
using NonConstFrom =
std::conditional_t, DecayedFrom *, DecayedFrom &>;

static inline bool isPossible(const From &f) {
return ForwardTo::isPossible(const_cast(f));
}

static inline decltype(auto) castFailed() { return ForwardTo::castFailed(); }

static inline decltype(auto) doCast(const From &f) {
return ForwardTo::doCast(const_cast(f));
}

static inline decltype(auto) doCastIfPossible(const From &f) {
return ForwardTo::doCastIfPossible(const_cast(f));
}
};
```

I have two solutions:

1. Change the return type to `To` (the first template parameter) like `NullableValueCastFailed` and `DefaultDoCastIfPossible`.
2. Compute the new return type:
- `T *` -> `const T *`
- `T &` -> `const T &`
- Otherwise, `T` -> `const T`

I will submit a patch to fix it. Please let me know which solution I should use (or, is there a better one I don't know?).

Contributor guide

Open the contributing guide

Research direction

Start from the ConstStrippingForwardingCast definition and inspect how its doCast and doCastIfPossible return types are used. Compare the return-type conventions in NullableValueCastFailed and DefaultDoCastIfPossible. Done means the cast adds const to the return type as intended without breaking existing forwarding behavior; the issue does not name a specific test file.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
56/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.