From ce6c25977b6c5891053d18f230e04900d6ff22f7 Mon Sep 17 00:00:00 2001 From: Nate Chandler Date: Thu, 8 Feb 2024 08:36:05 -0800 Subject: [PATCH] [BitwiseCopyable] Req conformance in decl module. Don't allow conformances to `BitwiseCopyable` to be declared in other modules. --- include/swift/AST/DiagnosticsSema.def | 3 +++ lib/Sema/TypeCheckBitwise.cpp | 7 +++++++ test/Sema/bitwise_copyable_resilience.swift | 5 +++++ 3 files changed, 15 insertions(+) diff --git a/include/swift/AST/DiagnosticsSema.def b/include/swift/AST/DiagnosticsSema.def index e129317afd6..76ae9bf9241 100644 --- a/include/swift/AST/DiagnosticsSema.def +++ b/include/swift/AST/DiagnosticsSema.def @@ -7629,6 +7629,9 @@ NOTE(add_nominal_bitwise_copyable_conformance,none, NOTE(add_generic_parameter_non_bitwise_copyable_conformance,none, "consider making generic parameter %0 conform to the 'BitwiseCopyable' protocol", (Type)) +ERROR(bitwise_copyable_outside_module,none, + "conformance to 'BitwiseCopyable' must occur in the same module as %kind0", + (const ValueDecl *)) // -- older ones below -- diff --git a/lib/Sema/TypeCheckBitwise.cpp b/lib/Sema/TypeCheckBitwise.cpp index ae159adf9cf..a577d91fb0e 100644 --- a/lib/Sema/TypeCheckBitwise.cpp +++ b/lib/Sema/TypeCheckBitwise.cpp @@ -373,6 +373,13 @@ bool swift::checkBitwiseCopyableConformance(ProtocolConformance *conformance, return false; } + // BitwiseCopyable must be added in the same source file. + auto conformanceDecl = conformanceDC->getAsDecl(); + if (conformanceDecl->getModuleContext() != nominal->getModuleContext()) { + conformanceDecl->diagnose(diag::bitwise_copyable_outside_module, nominal); + return true; + } + auto check = isImplicit ? BitwiseCopyableCheck::Implicit : BitwiseCopyableCheck::Explicit; diff --git a/test/Sema/bitwise_copyable_resilience.swift b/test/Sema/bitwise_copyable_resilience.swift index ff64714ce4b..160ac8375cf 100644 --- a/test/Sema/bitwise_copyable_resilience.swift +++ b/test/Sema/bitwise_copyable_resilience.swift @@ -27,6 +27,10 @@ case somebody(T) case noone } +public struct Integer { + var value: Int +} + //--- Downstream.swift import Library @@ -47,3 +51,4 @@ struct S_Explicit_With_Woopsional : _BitwiseCopyable { func passWoopsional(_ t: Woopsional) { take(t) } // expected-error {{type_does_not_conform_decl_owner}} // expected-note@-15 {{where_requirement_failure_one_subst}} +extension Integer : @retroactive _BitwiseCopyable {} // expected-error {{bitwise_copyable_outside_module}}