Skip to content

Commit 53d0ef8

Browse files
authored
[Serialization] Tweak deployment-target-too-new diagnostic (#18475)
Before: module file's minimum deployment target is iOS 12.0: /path/to/FooKit.swiftmodule After: compiling for iOS 11.0, but module 'FooKit' has a minimum deployment target of iOS 12.0: /path/to/FooKit.swiftmodule Also tweak the "incompatible target" error to include the module name. rdar://problem/35546499
1 parent 423d2c6 commit 53d0ef8

File tree

5 files changed

+71
-23
lines changed

5 files changed

+71
-23
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -632,17 +632,21 @@ ERROR(serialization_name_mismatch,Fatal,
632632
ERROR(serialization_name_mismatch_repl,none,
633633
"cannot load module '%0' as %1", (StringRef, Identifier))
634634
ERROR(serialization_target_incompatible,Fatal,
635-
"module file was created for incompatible target %0: %1",
636-
(StringRef, StringRef))
635+
"module %0 was created for incompatible target %1: %2",
636+
(Identifier, StringRef, StringRef))
637637
ERROR(serialization_target_incompatible_repl,none,
638-
"module file was created for incompatible target %0: %1",
639-
(StringRef, StringRef))
638+
"module %0 was created for incompatible target %1: %2",
639+
(Identifier, StringRef, StringRef))
640640
ERROR(serialization_target_too_new,Fatal,
641-
"module file's minimum deployment target is %0 v%1.%2%select{|.%3}3: %4",
642-
(StringRef, unsigned, unsigned, unsigned, StringRef))
641+
"compiling for %0 %1, but module %2 has a minimum "
642+
"deployment target of %0 %3: %4",
643+
(StringRef, clang::VersionTuple, Identifier, clang::VersionTuple,
644+
StringRef))
643645
ERROR(serialization_target_too_new_repl,none,
644-
"module file's minimum deployment target is %0 v%1.%2%select{|.%3}3: %4",
645-
(StringRef, unsigned, unsigned, unsigned, StringRef))
646+
"compiling for %0 %1, but module %2 has a minimum "
647+
"deployment target of %0 %3: %4",
648+
(StringRef, clang::VersionTuple, Identifier, clang::VersionTuple,
649+
StringRef))
646650

647651
ERROR(serialization_fatal,Fatal,
648652
"fatal error encountered while reading from module '%0'; "

lib/Serialization/SerializedModuleLoader.cpp

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,42 @@ findModule(ASTContext &ctx, AccessPathElem moduleID,
232232
moduleBuffer, moduleDocBuffer, scratch);
233233
}
234234

235+
static std::pair<StringRef, clang::VersionTuple>
236+
getOSAndVersionForDiagnostics(const llvm::Triple &triple) {
237+
StringRef osName;
238+
unsigned major, minor, micro;
239+
if (triple.isMacOSX()) {
240+
// macOS triples represent their versions differently, so we have to use the
241+
// special accessor.
242+
triple.getMacOSXVersion(major, minor, micro);
243+
osName = swift::prettyPlatformString(PlatformKind::OSX);
244+
} else {
245+
triple.getOSVersion(major, minor, micro);
246+
if (triple.isWatchOS()) {
247+
osName = swift::prettyPlatformString(PlatformKind::watchOS);
248+
} else if (triple.isTvOS()) {
249+
assert(triple.isiOS() &&
250+
"LLVM treats tvOS as a kind of iOS, so tvOS is checked first");
251+
osName = swift::prettyPlatformString(PlatformKind::tvOS);
252+
} else if (triple.isiOS()) {
253+
osName = swift::prettyPlatformString(PlatformKind::iOS);
254+
} else {
255+
assert(!triple.isOSDarwin() && "unknown Apple OS");
256+
// Fallback to the LLVM triple name. This isn't great (it won't be
257+
// capitalized or anything), but it's better than nothing.
258+
osName = triple.getOSName();
259+
}
260+
}
261+
262+
assert(!osName.empty());
263+
clang::VersionTuple version;
264+
if (micro != 0)
265+
version = clang::VersionTuple(major, minor, micro);
266+
else
267+
version = clang::VersionTuple(major, minor);
268+
return {osName, version};
269+
}
270+
235271
FileUnit *SerializedModuleLoader::loadAST(
236272
ModuleDecl &M, Optional<SourceLoc> diagLoc,
237273
std::unique_ptr<llvm::MemoryBuffer> moduleInputBuffer,
@@ -436,30 +472,26 @@ FileUnit *SerializedModuleLoader::loadAST(
436472
if (Ctx.LangOpts.DebuggerSupport)
437473
diagKind = diag::serialization_target_incompatible_repl;
438474
Ctx.Diags.diagnose(*diagLoc, diagKind,
439-
loadInfo.targetTriple, moduleBufferID);
475+
M.getName(), loadInfo.targetTriple, moduleBufferID);
440476
break;
441477
}
442478

443479
case serialization::Status::TargetTooNew: {
444480
llvm::Triple moduleTarget(llvm::Triple::normalize(loadInfo.targetTriple));
445481

446-
StringRef osName;
447-
unsigned major, minor, micro;
448-
if (moduleTarget.isMacOSX()) {
449-
osName = swift::prettyPlatformString(PlatformKind::OSX);
450-
moduleTarget.getMacOSXVersion(major, minor, micro);
451-
} else {
452-
osName = moduleTarget.getOSName();
453-
moduleTarget.getOSVersion(major, minor, micro);
454-
}
482+
std::pair<StringRef, clang::VersionTuple> moduleOSInfo =
483+
getOSAndVersionForDiagnostics(moduleTarget);
484+
std::pair<StringRef, clang::VersionTuple> compilationOSInfo =
485+
getOSAndVersionForDiagnostics(Ctx.LangOpts.Target);
455486

456487
// FIXME: This doesn't handle a non-debugger REPL, which should also treat
457488
// this as a non-fatal error.
458489
auto diagKind = diag::serialization_target_too_new;
459490
if (Ctx.LangOpts.DebuggerSupport)
460491
diagKind = diag::serialization_target_too_new_repl;
461492
Ctx.Diags.diagnose(*diagLoc, diagKind,
462-
osName, major, minor, micro, moduleBufferID);
493+
compilationOSInfo.first, compilationOSInfo.second,
494+
M.getName(), moduleOSInfo.second, moduleBufferID);
463495
break;
464496
}
465497
}

test/Serialization/target-incompatible.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
// RUN: not %target-swift-frontend -I %t -typecheck -parse-stdlib %s -DSOLARIS 2>&1 | %FileCheck -check-prefix=CHECK-SOLARIS %s
1010

1111
#if MIPS
12-
// CHECK-MIPS: :[[@LINE+1]]:8: error: module file was created for incompatible target mips64-unknown-darwin14: {{.*}}mips.swiftmodule{{$}}
12+
// CHECK-MIPS: :[[@LINE+1]]:8: error: module 'mips' was created for incompatible target mips64-unknown-darwin14: {{.*}}mips.swiftmodule{{$}}
1313
import mips
1414

1515
#elseif SOLARIS
16-
// CHECK-SOLARIS: :[[@LINE+1]]:8: error: module file was created for incompatible target x86_64-unknown-solaris8: {{.*}}solaris.swiftmodule{{$}}
16+
// CHECK-SOLARIS: :[[@LINE+1]]:8: error: module 'solaris' was created for incompatible target x86_64-unknown-solaris8: {{.*}}solaris.swiftmodule{{$}}
1717
import solaris
1818

1919
#endif
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend -target x86_64-apple-ios50.50.1 -emit-module -parse-stdlib %S/../Inputs/empty.swift -o %t
3+
// RUN: not %target-swift-frontend -parse-stdlib -target x86_64-apple-ios12 -I %t -typecheck %s 2>&1 | %FileCheck %s
4+
// RUN: %target-swift-frontend -parse-stdlib -target x86_64-apple-ios12 -I %t -typecheck %s -disable-target-os-checking
5+
// RUN: %target-swift-frontend -parse-stdlib -target x86_64-apple-ios50.50.1 -I %t -typecheck %s
6+
// RUN: %target-swift-frontend -parse-stdlib -target x86_64-apple-ios50.51 -I %t -typecheck %s
7+
8+
// REQUIRES: OS=ios
9+
10+
// CHECK: :[[@LINE+1]]:8: error: compiling for iOS 12.0, but module 'empty' has a minimum deployment target of iOS 50.50.1: {{.*}}empty.swiftmodule{{$}}
11+
import empty
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
// RUN: %empty-directory(%t)
22
// RUN: %target-swift-frontend -target x86_64-apple-macosx10.50 -emit-module -parse-stdlib %S/../Inputs/empty.swift -o %t
3-
// RUN: not %target-swift-frontend -I %t -typecheck %s 2>&1 | %FileCheck %s
3+
// RUN: not %target-swift-frontend -I %t -target x86_64-apple-macosx10.9 -typecheck %s 2>&1 | %FileCheck %s
4+
// RUN: not %target-swift-frontend -I %t -target x86_64-apple-darwin13 -typecheck %s 2>&1 | %FileCheck %s
45
// RUN: %target-swift-frontend -I %t -typecheck %s -disable-target-os-checking
56
// RUN: %target-swift-frontend -target x86_64-apple-macosx10.50 -I %t -typecheck %s
67
// RUN: %target-swift-frontend -target x86_64-apple-macosx10.50.1 -I %t -typecheck %s
78

89
// REQUIRES: OS=macosx
910

10-
// CHECK: :[[@LINE+1]]:8: error: module file's minimum deployment target is OS X v10.50: {{.*}}empty.swiftmodule{{$}}
11+
// CHECK: :[[@LINE+1]]:8: error: compiling for OS X 10.9, but module 'empty' has a minimum deployment target of OS X 10.50: {{.*}}empty.swiftmodule{{$}}
1112
import empty

0 commit comments

Comments
 (0)