[clang] [clang][bytecode] Diagnose unknown reference params pre-C++23 (PR #175013)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Jan 8 08:22:55 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Timm Baeder (tbaederr)
<details>
<summary>Changes</summary>
Otherwise, we will ultimately create dummy pointers for them and possibly not diagnose anything.
---
Full diff: https://github.com/llvm/llvm-project/pull/175013.diff
3 Files Affected:
- (modified) clang/lib/AST/ByteCode/Compiler.cpp (+4)
- (modified) clang/lib/AST/ByteCode/Interp.cpp (+3-2)
- (modified) clang/test/AST/ByteCode/cxx23.cpp (+17)
``````````diff
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index e85ccafc0e53b..cbee7691565a9 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -7076,6 +7076,10 @@ bool Compiler<Emitter>::visitDeclRef(const ValueDecl *D, const Expr *E) {
return this->emitGetPtrParam(It->second.Offset, E);
}
+
+ if (!Ctx.getLangOpts().CPlusPlus23 && IsReference)
+ return this->emitInvalidDeclRef(cast<DeclRefExpr>(E),
+ /*InitializerFailed=*/false, E);
}
// Local variables.
if (auto It = Locals.find(D); It != Locals.end()) {
diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index 6444025d6fcf8..5394466dd72e3 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -144,9 +144,10 @@ static bool diagnoseUnknownDecl(InterpState &S, CodePtr OpPC,
if (isa<ParmVarDecl>(D)) {
if (D->getType()->isReferenceType()) {
if (S.inConstantContext() && S.getLangOpts().CPlusPlus &&
- !S.getLangOpts().CPlusPlus11)
+ !S.getLangOpts().CPlusPlus11) {
diagnoseNonConstVariable(S, OpPC, D);
- return false;
+ return false;
+ }
}
const SourceInfo &Loc = S.Current->getSource(OpPC);
diff --git a/clang/test/AST/ByteCode/cxx23.cpp b/clang/test/AST/ByteCode/cxx23.cpp
index 4a4ff4b48e8b5..4b87f44d14345 100644
--- a/clang/test/AST/ByteCode/cxx23.cpp
+++ b/clang/test/AST/ByteCode/cxx23.cpp
@@ -520,3 +520,20 @@ namespace InactiveLocalsInConditionalOp {
}
#endif
+
+namespace UnknownParams {
+ class X {
+ public:
+ constexpr operator bool(){ return true; }
+ };
+ int foo(X x) {
+ static_assert(x);
+ return 1;
+ }
+
+ int foo2(X &x) { // all20-note {{declared here}}
+ static_assert(x); // all20-error {{not an integral constant expression}} \
+ // all20-note {{function parameter 'x' with unknown value cannot be used in a constant expression}}
+ return 1;
+ }
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/175013
More information about the cfe-commits
mailing list