Skip to content

Commit fb55ab8

Browse files
committed
Ensure that #fileID wraps raw identifier module names in backticks.
`#fileID` never accounted for the possibility that someone one have a module alias _itself_, so it always generated the module's real (physical) name. This _technically_ changes the behavior of `#fileID` for self-aliased modules, but since nobody would have ever had a reason to do that before raw identifiers, it's unlikely that this change would affect anyone in practice.
1 parent 48b71a7 commit fb55ab8

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

lib/AST/Module.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3338,7 +3338,19 @@ getInfoForUsedFileNames(const ModuleDecl *module) {
33383338

33393339
static void computeFileID(const ModuleDecl *module, StringRef name,
33403340
SmallVectorImpl<char> &result) {
3341-
result.assign(module->getNameStr().begin(), module->getNameStr().end());
3341+
// The module might alias itself (e.g., to use a raw identifier as the name
3342+
// that it references itself with in its own source code), so we need to look
3343+
// that up.
3344+
Identifier moduleName = module->getASTContext().getRealModuleName(
3345+
module->getName(),
3346+
ASTContext::ModuleAliasLookupOption::aliasFromRealName);
3347+
if (moduleName.mustAlwaysBeEscaped()) {
3348+
result.push_back('`');
3349+
result.append(moduleName.str().begin(), moduleName.str().end());
3350+
result.push_back('`');
3351+
} else {
3352+
result.assign(moduleName.str().begin(), moduleName.str().end());
3353+
}
33423354
result.push_back('/');
33433355
result.append(name.begin(), name.end());
33443356
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Tests that `#fileID` has the correct module name when a module aliases
2+
// itself (to internally refer to itself a raw identifier name instead of the
3+
// -module-name, which must be filesystem-friendly).
4+
5+
// RUN: %target-swift-frontend -module-name Original -module-alias "^raw*identifier^=Original" %s -emit-silgen | %FileCheck %s
6+
7+
print("#fileID = \(#fileID)")
8+
// CHECK: %{{[0-9]+}} = string_literal utf8 "`^raw*identifier^`/fileid-raw-identifier-module-name.swift"
9+
// CHECK-NOT: %{{[0-9]+}} = string_literal utf8 "Original/fileid-raw-identifier-module-name.swift"

0 commit comments

Comments
 (0)