Skip to content

Commit 003673b

Browse files
authored
Fix #4938 - Be stricter wrt. supported AddrExp in toConstElem() (#4939)
DMD seems to support AddrExp of struct literals only...
1 parent fec7371 commit 003673b

File tree

2 files changed

+5
-40
lines changed

2 files changed

+5
-40
lines changed

gen/toconstelem.cpp

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -392,40 +392,6 @@ class ToConstElemVisitor : public Visitor {
392392
IF_LOG Logger::println("AddrExp::toConstElem: %s @ %s", e->toChars(),
393393
e->type->toChars());
394394
LOG_SCOPE;
395-
// FIXME: this should probably be generalized more so we don't
396-
// need to have a case for each thing we can take the address of
397-
398-
// address of global variable
399-
if (auto vexp = e->e1->isVarExp()) {
400-
result = DtoConstSymbolAddress(e->loc, vexp->var);
401-
return;
402-
}
403-
404-
// address of indexExp
405-
if (auto iexp = e->e1->isIndexExp()) {
406-
// indexee must be global static array var
407-
VarExp *vexp = iexp->e1->isVarExp();
408-
assert(vexp);
409-
VarDeclaration *vd = vexp->var->isVarDeclaration();
410-
assert(vd);
411-
assert(vd->type->toBasetype()->ty == TY::Tsarray);
412-
DtoResolveVariable(vd);
413-
assert(isIrGlobalCreated(vd));
414-
415-
// get index
416-
LLConstant *index = toConstElem(iexp->e2, p);
417-
assert(index->getType() == DtoSize_t());
418-
419-
// gep
420-
LLConstant *idxs[2] = {DtoConstSize_t(0), index};
421-
LLConstant *val = isaConstant(getIrGlobal(vd)->value);
422-
LLConstant *gep = llvm::ConstantExpr::getGetElementPtr(
423-
DtoType(vd->type), val, idxs, true);
424-
425-
assert(e->type->toBasetype()->ty == TY::Tpointer);
426-
result = gep;
427-
return;
428-
}
429395

430396
if (auto se = e->e1->isStructLiteralExp()) {
431397
result = p->getStructLiteralGlobal(se);
@@ -449,12 +415,7 @@ class ToConstElemVisitor : public Visitor {
449415
return;
450416
}
451417

452-
if (e->e1->op == EXP::slice || e->e1->op == EXP::dotVariable) {
453-
visit(static_cast<Expression *>(e));
454-
return;
455-
}
456-
457-
llvm_unreachable("unsupported AddrExp in ToConstElemVisitor");
418+
visit(static_cast<Expression *>(e));
458419
}
459420

460421
//////////////////////////////////////////////////////////////////////////////

tests/fail_compilation/gh4938.d

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// RUN: not %ldc -c %s 2>&1 | FileCheck %s
2+
3+
// CHECK: gh4938.d(4): Error: expression `&"whoops"w[0]` is not a constant
4+
immutable(wchar)* x = &"whoops"w[0];

0 commit comments

Comments
 (0)