@@ -604,35 +604,43 @@ std::optional<std::string>
604604SerializedModuleBaseName::getPackageInterfacePathIfInSamePackage (
605605 llvm::vfs::FileSystem &fs, ASTContext &ctx) const {
606606 if (!ctx.LangOpts .EnablePackageInterfaceLoad )
607- return {} ;
607+ return std:: nullopt ;
608608
609609 std::string packagePath{
610- getName (file_types::TY_PackageSwiftModuleInterfaceFile)};
610+ getName (file_types::TY_PackageSwiftModuleInterfaceFile)};
611611
612612 if (fs.exists (packagePath)) {
613613 // Read the interface file and extract its package-name argument value
614- std::string result;
615- if (auto packageFile = llvm::MemoryBuffer::getFile (packagePath)) {
616- llvm::BumpPtrAllocator alloc;
617- llvm::StringSaver argSaver (alloc);
618- SmallVector<const char *, 8 > args;
619- (void )extractCompilerFlagsFromInterface (packagePath,
620- (*packageFile)->getBuffer (), argSaver, args);
621- for (unsigned I = 0 , N = args.size (); I + 1 < N; I++) {
622- StringRef current (args[I]), next (args[I + 1 ]);
623- if (current == " -package-name" ) {
624- // Instead of `break` here, continue to get the last value in case of dupes,
625- // to be consistent with the default parsing logic.
626- result = next;
627- }
614+ if (auto packageName = getPackageNameFromInterface (packagePath, fs)) {
615+ // Return the .package.swiftinterface path if the package name applies to
616+ // the importer module.
617+ if (*packageName == ctx.LangOpts .PackageName )
618+ return packagePath;
619+ }
620+ }
621+ return std::nullopt ;
622+ }
623+
624+ std::optional<std::string>
625+ SerializedModuleBaseName::getPackageNameFromInterface (
626+ StringRef interfacePath, llvm::vfs::FileSystem &fs) const {
627+ std::optional<std::string> result;
628+ if (auto interfaceFile = fs.getBufferForFile (interfacePath)) {
629+ llvm::BumpPtrAllocator alloc;
630+ llvm::StringSaver argSaver (alloc);
631+ SmallVector<const char *, 8 > args;
632+ (void )extractCompilerFlagsFromInterface (
633+ interfacePath, (*interfaceFile)->getBuffer (), argSaver, args);
634+ for (unsigned I = 0 , N = args.size (); I + 1 < N; I++) {
635+ StringRef current (args[I]), next (args[I + 1 ]);
636+ if (current == " -package-name" ) {
637+ // Instead of `break` here, continue to get the last value in case of
638+ // dupes, to be consistent with the default parsing logic.
639+ result = next;
628640 }
629641 }
630- // Return the .package.swiftinterface path if the package name applies to
631- // the importer module.
632- if (!result.empty () && result == ctx.LangOpts .PackageName )
633- return packagePath;
634642 }
635- return {} ;
643+ return result ;
636644}
637645
638646std::optional<std::string>
@@ -642,16 +650,18 @@ SerializedModuleBaseName::findInterfacePath(llvm::vfs::FileSystem &fs,
642650 // Ensure the public swiftinterface already exists, otherwise bail early
643651 // as it's considered the module doesn't exist.
644652 if (!fs.exists (interfacePath))
645- return {};
646-
647- // Check if a package interface exists and if the package name applies to
648- // the importer module.
649- auto pkgPath = getPackageInterfacePathIfInSamePackage (fs, ctx).value_or (" " );
650- if (!pkgPath.empty () && fs.exists (pkgPath))
651- return pkgPath;
653+ return std::nullopt ;
654+
655+ // If in the same package, try return the package interface path if not
656+ // preferring the interface file.
657+ if (!ctx.LangOpts .AllowNonPackageInterfaceImportFromSamePackage ) {
658+ if (auto packageName = getPackageNameFromInterface (interfacePath, fs)) {
659+ if (*packageName == ctx.LangOpts .PackageName )
660+ return getPackageInterfacePathIfInSamePackage (fs, ctx);
661+ }
662+ }
652663
653- // If above fails, use the existing logic as fallback.
654- // If present, use the private interface instead of the public one.
664+ // Otherwise, use the private interface instead of the public one.
655665 std::string privatePath{
656666 getName (file_types::TY_PrivateSwiftModuleInterfaceFile)};
657667 if (fs.exists (privatePath))
@@ -704,35 +714,33 @@ bool SerializedModuleLoaderBase::findModule(
704714 std::optional<SerializedModuleBaseName> firstAbsoluteBaseName;
705715
706716 for (const auto &targetSpecificBaseName : targetSpecificBaseNames) {
707- SerializedModuleBaseName
708- absoluteBaseName{currPath, targetSpecificBaseName};
717+ SerializedModuleBaseName absoluteBaseName{currPath,
718+ targetSpecificBaseName};
709719
710720 if (!firstAbsoluteBaseName.has_value ())
711721 firstAbsoluteBaseName.emplace (absoluteBaseName);
712722
713723 auto result = findModuleFilesInDirectory (
714724 moduleID, absoluteBaseName, moduleInterfacePath,
715725 moduleInterfaceSourcePath, moduleBuffer, moduleDocBuffer,
716- moduleSourceInfoBuffer, skipBuildingInterface,
717- IsFramework, isTestableDependencyLookup);
718- if (!result) {
726+ moduleSourceInfoBuffer, skipBuildingInterface, IsFramework,
727+ isTestableDependencyLookup);
728+ if (!result)
719729 return SearchResult::Found;
720- } else if (result == std::errc::not_supported) {
730+ if (result == std::errc::not_supported)
721731 return SearchResult::Error;
722- } else if (result != std::errc::no_such_file_or_directory) {
732+ if (result != std::errc::no_such_file_or_directory)
723733 return SearchResult::NotFound;
724- }
725734 }
726735
727736 // We can only get here if all targetFileNamePairs failed with
728737 // 'std::errc::no_such_file_or_directory'.
729- if (firstAbsoluteBaseName
730- && maybeDiagnoseTargetMismatch (moduleID.Loc , moduleName,
731- *firstAbsoluteBaseName)) {
738+ if (firstAbsoluteBaseName &&
739+ maybeDiagnoseTargetMismatch (moduleID.Loc , moduleName,
740+ *firstAbsoluteBaseName))
732741 return SearchResult::Error;
733- } else {
734- return SearchResult::NotFound;
735- }
742+
743+ return SearchResult::NotFound;
736744 };
737745
738746 SmallVector<std::string, 4 > InterestingFilenames = {
@@ -788,13 +796,11 @@ bool SerializedModuleLoaderBase::findModule(
788796 moduleID, absoluteBaseName, moduleInterfacePath,
789797 moduleInterfaceSourcePath, moduleBuffer, moduleDocBuffer,
790798 moduleSourceInfoBuffer, skipBuildingInterface, isFramework);
791- if (!result) {
799+ if (!result)
792800 return true ;
793- } else if (result == std::errc::not_supported) {
801+ if (result == std::errc::not_supported)
794802 return false ;
795- } else {
796- continue ;
797- }
803+ continue ;
798804 }
799805 case ModuleSearchPathKind::Framework:
800806 case ModuleSearchPathKind::DarwinImplicitFramework: {
0 commit comments