Skip to content

Commit ea70d37

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 60513d7 commit ea70d37

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
@@ -3332,7 +3332,19 @@ getInfoForUsedFileNames(const ModuleDecl *module) {
33323332

33333333
static void computeFileID(const ModuleDecl *module, StringRef name,
33343334
SmallVectorImpl<char> &result) {
3335-
result.assign(module->getNameStr().begin(), module->getNameStr().end());
3335+
// The module might alias itself (e.g., to use a raw identifier as the name
3336+
// that it references itself with in its own source code), so we need to look
3337+
// that up.
3338+
Identifier moduleName = module->getASTContext().getRealModuleName(
3339+
module->getName(),
3340+
ASTContext::ModuleAliasLookupOption::aliasFromRealName);
3341+
if (moduleName.mustAlwaysBeEscaped()) {
3342+
result.push_back('`');
3343+
result.append(moduleName.str().begin(), moduleName.str().end());
3344+
result.push_back('`');
3345+
} else {
3346+
result.assign(moduleName.str().begin(), moduleName.str().end());
3347+
}
33363348
result.push_back('/');
33373349
result.append(name.begin(), name.end());
33383350
}
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)