Skip to content
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

Improve error msg on global var collision #3081

Merged
merged 1 commit into from
May 31, 2019
Merged
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
14 changes: 10 additions & 4 deletions gen/llvmhelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1757,14 +1757,20 @@ llvm::GlobalVariable *declareGlobal(const Loc &loc, llvm::Module &module,
const auto existingType = existing->getType()->getElementType();
if (existingType != type || existing->isConstant() != isConstant ||
existing->isThreadLocal() != isThreadLocal) {
const auto existingTypeName = llvmTypeToString(existingType);
const auto newTypeName = llvmTypeToString(type);
error(loc,
"Global variable type does not match previous declaration with "
"same mangled name: `%s`",
mangledName.str().c_str());
errorSupplemental(loc, "Previous IR type: %s", existingTypeName.c_str());
errorSupplemental(loc, "New IR type: %s", newTypeName.c_str());
const auto suppl = [&loc](const char *prefix, LLType *type,
bool isConstant, bool isThreadLocal) {
const auto typeName = llvmTypeToString(type);
errorSupplemental(loc, "%s %s, %s, %s", prefix, typeName.c_str(),
isConstant ? "const" : "mutable",
isThreadLocal ? "thread-local" : "non-thread-local");
};
suppl("Previous IR type:", existingType, existing->isConstant(),
existing->isThreadLocal());
suppl("New IR type: ", type, isConstant, isThreadLocal);
fatal();
}
return existing;
Expand Down
9 changes: 9 additions & 0 deletions tests/fail_compilation/global_var_collision.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: not %ldc -c %s 2>&1 | FileCheck %s

extern(C) extern int myGlobal;

// CHECK: global_var_collision.d(9): Error: Global variable type does not match previous declaration with same mangled name: `myGlobal`
// CHECK-NEXT: Previous IR type: i32, mutable, thread-local
// CHECK-NEXT: New IR type: i64, const, non-thread-local
pragma(mangle, myGlobal.mangleof)
extern(C) __gshared const long myGlobal2 = 123;