Skip to content

Commit 66d64b6

Browse files
committed
Updates params to ASTContext::getRealModuleName
1 parent daf07c0 commit 66d64b6

File tree

5 files changed

+35
-19
lines changed

5 files changed

+35
-19
lines changed

include/swift/AST/ASTContext.h

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -491,13 +491,21 @@ class ASTContext final {
491491

492492
/// Look up the module alias map by the given \p key.
493493
///
494-
/// \param key A module alias or real name to look up the map by.
495-
/// \param reverseLookup Default to false, but if true, it will treat the \p key as a real name and
496-
/// look up the alias, which can be used to guard against real names appearing in source files.
497-
/// \returns The real name or alias mapped to the key.
498-
/// If \p reverseLookup is true but the \p key is an alias, it will return an empty Identifier.
499-
/// If no aliasing is used, \p key will be returned.
500-
Identifier getRealModuleName(Identifier key, bool reverseLookup = false) const;
494+
/// \param key A module alias or real name to look up the map by
495+
/// \param alwaysReturnRealName Indicates whether it should always retrieve the real module name
496+
/// given \p key. Defaults to true. This takes a higher precedence than
497+
/// \p lookupAliasFromReal.
498+
/// \param lookupAliasFromReal Indicates whether to look up an alias by treating \p key
499+
/// as a real name. Defaults to false.
500+
/// \return The real name or alias mapped to the key.
501+
/// If \p alwaysReturnRealName is true, return the real module name if \p key is an alias
502+
/// or the key itself since that's the real name.
503+
/// If \p lookupAliasFromReal is true, and \p alwaysReturnRealName is false, return
504+
/// only if \p key is a real name, else an empty Identifier.
505+
/// If no aliasing is used, return \p key.
506+
Identifier getRealModuleName(Identifier key,
507+
bool alwaysReturnRealName = true,
508+
bool lookupAliasFromReal = false) const;
501509

502510
/// Decide how to interpret two precedence groups.
503511
Associativity associateInfixOperators(PrecedenceGroupDecl *left,

lib/AST/ASTContext.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1653,19 +1653,25 @@ void ASTContext::setModuleAliases(const llvm::StringMap<StringRef> &aliasMap) {
16531653
}
16541654
}
16551655

1656-
Identifier ASTContext::getRealModuleName(Identifier key, bool reverseLookup) const {
1656+
Identifier ASTContext::getRealModuleName(Identifier key, bool alwaysReturnRealName, bool lookupAliasFromReal) const {
16571657
auto found = ModuleAliasMap.find(key);
16581658
if (found == ModuleAliasMap.end())
16591659
return key;
16601660

16611661
// Found an entry
16621662
auto realOrAlias = found->second;
1663-
// If reverseLookup, i.e. look up an alias by real name, but the found
1664-
// entry is keyed by an alias, return an empty Identifier
1665-
if (reverseLookup && realOrAlias.second)
1666-
return Identifier();
16671663

1668-
// Return a real name or an alias (if reverseLookup) mapped to the given key
1664+
// If alwaysReturnRealName, return the real name if the key is an
1665+
// alias or the key itself since that's the real name
1666+
if (alwaysReturnRealName) {
1667+
return realOrAlias.second ? realOrAlias.first : key;
1668+
}
1669+
1670+
// If lookupAliasFromReal, and the found entry should be keyed by a real
1671+
// name, return the entry value, otherwise, return an empty Identifier.
1672+
if (lookupAliasFromReal == realOrAlias.second)
1673+
return Identifier();
1674+
16691675
return realOrAlias.first;
16701676
}
16711677

lib/AST/UnqualifiedLookup.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -437,9 +437,11 @@ void UnqualifiedLookupFactory::lookForAModuleWithTheGivenName(
437437
// Check if the given name appearing in the source file is a module
438438
// real name or alias; for example, if `-module-alias Foo=Bar` was
439439
// passed, the alias 'Foo' should appear in source files, not 'Bar'.
440-
// If no module aliasing is used, this will simply return the given
441-
// name and 'true' indicating the check passed.
442-
if (!Ctx.getRealModuleName(givenName).empty()) {
440+
// If the real name 'Bar' was used, looking up getRealModuleName will
441+
// return an empty Identifier.
442+
if (!Ctx.getRealModuleName(givenName, /*alwaysReturnRealName=*/false).empty()) {
443+
// Only load the module if the lookup value is not empty, i.e. given
444+
// name is a module alias, not a real module name.
443445
desiredModule = Ctx.getLoadedModule(givenName);
444446
}
445447

lib/Parse/ParseDecl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4823,10 +4823,10 @@ ParserResult<ImportDecl> Parser::parseDeclImport(ParseDeclOptions Flags,
48234823
// and check that the module alias appeared in source files instead of
48244824
// its corresponding real name
48254825
auto parsedModuleID = importPath.get().front().Item;
4826-
if (Context.getRealModuleName(parsedModuleID).empty()) {
4826+
if (Context.getRealModuleName(parsedModuleID, /*alwaysReturnRealName=*/false).empty()) {
48274827
// If reached here, it means the parsed module name is a real module name
48284828
// which appeared in the source file; only a module alias should be allowed
4829-
auto aliasName = Context.getRealModuleName(parsedModuleID, /*reverseLookup=*/true);
4829+
auto aliasName = Context.getRealModuleName(parsedModuleID, /*alwaysReturnRealName=*/false, /*lookupAliasFromReal=*/true);
48304830
diagnose(importPath.front().Loc, diag::expected_module_alias,
48314831
parsedModuleID, aliasName)
48324832
.fixItReplace(importPath.front().Loc, aliasName.str());

test/Frontend/module-alias-diags.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
/// Create module Lib that imports XLogging WITH -module-alias XLogging=AppleLogging
2727
// RUN: not %target-swift-frontend -module-name LibC %t/FileLibImportRealName.swift -module-alias XLogging=AppleLogging -I %t -emit-module -emit-module-path %t/LibC.swiftmodule 2> %t/result-LibC.output
2828
// RUN: %FileCheck %s -input-file %t/result-LibC.output -check-prefix CHECK-NOT-IMPORT
29-
// CHECK-NOT-IMPORT: error: module real name should not appear in source files; only the module alias is allowed
29+
// CHECK-NOT-IMPORT: error: cannot refer to module as 'AppleLogging' because it has been aliased; use 'XLogging' instead
3030

3131
/// 4-1. Fail: referencing the real module name that's aliased should fail
3232
/// Create module Lib that imports XLogging WITH -module-alias XLogging=AppleLogging

0 commit comments

Comments
 (0)