Skip to content

[clang][Interp] Fall back to dummy pointers if createGlobal() fails #102464

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions clang/lib/AST/Interp/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3617,7 +3617,7 @@ VarCreationState Compiler<Emitter>::visitDecl(const VarDecl *VD) {

auto R = this->visitVarDecl(VD, /*Toplevel=*/true);

if (R.notCreated())
if (R.notCreated() || R.dummyCreated())
return R;

if (R)
Expand Down Expand Up @@ -3709,7 +3709,7 @@ VarCreationState Compiler<Emitter>::visitVarDecl(const VarDecl *VD, bool Topleve
// This case is EvalEmitter-only. If we won't create any instructions for the
// initializer anyway, don't bother creating the variable in the first place.
if (!this->isActive())
return VarCreationState::NotCreated();
return VarCreationState::NotCreated;

const Expr *Init = VD->getInit();
std::optional<PrimType> VarT = classify(VD->getType());
Expand Down Expand Up @@ -3759,12 +3759,16 @@ VarCreationState Compiler<Emitter>::visitVarDecl(const VarDecl *VD, bool Topleve
return Init && checkDecl() && initGlobal(*GlobalIndex);
}

std::optional<unsigned> GlobalIndex = P.createGlobal(VD, Init);
if (std::optional<unsigned> GlobalIndex = P.createGlobal(VD, Init))
return !Init || (checkDecl() && initGlobal(*GlobalIndex));

if (!GlobalIndex)
return false;
if (std::optional<unsigned> I = P.getOrCreateDummy(VD)) {
if (!this->emitGetPtrGlobal(*I, VD))
return false;
return VarCreationState::DummyCreated;
}

return !Init || (checkDecl() && initGlobal(*GlobalIndex));
return false;
} else {
InitLinkScope<Emitter> ILS(this, InitLink::Decl(VD));

Expand Down Expand Up @@ -5240,7 +5244,7 @@ bool Compiler<Emitter>::visitDeclRef(const ValueDecl *D, const Expr *E) {
auto revisit = [&](const VarDecl *VD) -> bool {
auto VarState = this->visitDecl(VD);

if (VarState.notCreated())
if (VarState.notCreated() || VarState.dummyCreated())
return true;
if (!VarState)
return false;
Expand Down
11 changes: 6 additions & 5 deletions clang/lib/AST/Interp/Compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,14 @@ struct InitLink {
/// State encapsulating if a the variable creation has been successful,
/// unsuccessful, or no variable has been created at all.
struct VarCreationState {
std::optional<bool> S = std::nullopt;
enum SS { Failure = 0, Success = 1, NotCreated, DummyCreated } S;
VarCreationState() = default;
VarCreationState(bool b) : S(b) {}
static VarCreationState NotCreated() { return VarCreationState(); }
VarCreationState(SS b) : S(b) {}
VarCreationState(bool b) : S(b ? Success : Failure) {}

operator bool() const { return S && *S; }
bool notCreated() const { return !S; }
operator bool() const { return S == Success; }
bool notCreated() const { return S == NotCreated; }
bool dummyCreated() const { return S == DummyCreated; }
};

/// Compilation context for expressions.
Expand Down
10 changes: 10 additions & 0 deletions clang/test/AST/Interp/records.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1576,3 +1576,13 @@ namespace ctorOverrider {
constexpr Covariant1 cb;
}
#endif

namespace IncompleteStaticStructMember {
struct Foo;
struct Bar {
static const Foo x;
static const Foo y;
};
static_assert(&Bar::x != nullptr, ""); // both-warning {{always true}}
static_assert(&Bar::x != &Bar::y, "");
}
Loading