Skip to content

Commit fefbbb6

Browse files
committed
[Sema] Note that the missing import is added automatically
1 parent 9e3f5f9 commit fefbbb6

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5849,6 +5849,10 @@ ERROR(inlinable_typealias_desugars_to_type_from_hidden_module,
58495849
"%4 was not imported by this file}5",
58505850
(DeclName, StringRef, StringRef, unsigned, Identifier, unsigned))
58515851

5852+
NOTE(missing_import_inserted,
5853+
none, "The missing import of module %0 will be added implicitly",
5854+
(Identifier))
5855+
58525856
ERROR(availability_macro_in_inlinable, none,
58535857
"availability macro cannot be used in " FRAGILE_FUNC_KIND "0",
58545858
(unsigned))

lib/Sema/ResilienceDiagnostics.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ static bool diagnoseTypeAliasDeclRefExportability(SourceLoc loc,
144144
}
145145
D->diagnose(diag::kind_declared_here, DescriptiveDeclKind::Type);
146146

147+
if (originKind == DisallowedOriginKind::ImplicitlyImported &&
148+
!ctx.LangOpts.isSwiftVersionAtLeast(6))
149+
ctx.Diags.diagnose(loc, diag::missing_import_inserted,
150+
definingModule->getName());
151+
147152
return true;
148153
}
149154

@@ -190,7 +195,13 @@ static bool diagnoseValueDeclRefExportability(SourceLoc loc, const ValueDecl *D,
190195
D->getDescriptiveKind(), D->getName(),
191196
fragileKind.getSelector(), definingModule->getName(),
192197
static_cast<unsigned>(originKind));
198+
199+
if (originKind == DisallowedOriginKind::ImplicitlyImported &&
200+
downgradeToWarning == DowngradeToWarning::Yes)
201+
ctx.Diags.diagnose(loc, diag::missing_import_inserted,
202+
definingModule->getName());
193203
}
204+
194205
return true;
195206
}
196207

@@ -244,5 +255,10 @@ TypeChecker::diagnoseConformanceExportability(SourceLoc loc,
244255
.warnUntilSwiftVersionIf(useConformanceAvailabilityErrorsOption &&
245256
!ctx.LangOpts.EnableConformanceAvailabilityErrors,
246257
6);
258+
259+
if (originKind == DisallowedOriginKind::ImplicitlyImported &&
260+
!ctx.LangOpts.isSwiftVersionAtLeast(6))
261+
ctx.Diags.diagnose(loc, diag::missing_import_inserted,
262+
M->getName());
247263
return true;
248264
}

test/Sema/implicit-import-in-inlinable-code.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,13 @@ import libA
5353
@inlinable public func bar() {
5454
let a = ImportedType()
5555
a.implicitlyImportedMethod() // expected-warning {{instance method 'implicitlyImportedMethod()' cannot be used in an '@inlinable' function because 'libB' was not imported by this file; this is an error in Swift 6}}
56+
// expected-note@-1 {{The missing import of module 'libB' will be added implicitly}}
5657

5758
// Expected implicit imports are still fine
5859
a.localModuleMethod()
5960

6061
conformanceUse(a) // expected-warning {{cannot use conformance of 'ImportedType' to 'SomeProtocol' here; 'libB' was not imported by this file; this is an error in Swift 6}}
62+
// expected-note@-1 {{The missing import of module 'libB' will be added implicitly}}
6163
}
6264

6365
// BEGIN clientFileA-OldCheck.swift
@@ -72,6 +74,7 @@ import libA
7274
a.localModuleMethod()
7375

7476
conformanceUse(a) // expected-warning {{cannot use conformance of 'ImportedType' to 'SomeProtocol' here; 'libB' was not imported by this file; this is an error in Swift 6}}
77+
// expected-note@-1 {{The missing import of module 'libB' will be added implicitly}}
7578
}
7679

7780
// BEGIN clientFileA-Swift6.swift

test/Sema/implicit-import-typealias.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,28 @@ public typealias WrapperAlias = Wrapper
6060

6161
import Aliases
6262

63-
// expected-warning@+1 {{'ClazzAlias' aliases 'Original.Clazz' and cannot be used here because 'Original' was not imported by this file; this is an error in Swift 6}}
63+
// expected-warning@+2 {{'ClazzAlias' aliases 'Original.Clazz' and cannot be used here because 'Original' was not imported by this file; this is an error in Swift 6}}
64+
// expected-note@+1 {{The missing import of module 'Original' will be added implicitly}}
6465
public class InheritsFromClazzAlias: ClazzAlias {}
6566

6667
@inlinable public func inlinableFunc() {
67-
// expected-warning@+1 {{'StructAlias' aliases 'Original.Struct' and cannot be used in an '@inlinable' function because 'Original' was not imported by this file; this is an error in Swift 6}}
68+
// expected-warning@+2 {{'StructAlias' aliases 'Original.Struct' and cannot be used in an '@inlinable' function because 'Original' was not imported by this file; this is an error in Swift 6}}
69+
// expected-note@+1 {{The missing import of module 'Original' will be added implicitly}}
6870
_ = StructAlias.self
6971
}
7072

71-
// expected-warning@+1 {{'ProtoAlias' aliases 'Original.Proto' and cannot be used here because 'Original' was not imported by this file; this is an error in Swift 6}}
73+
// expected-warning@+2 {{'ProtoAlias' aliases 'Original.Proto' and cannot be used here because 'Original' was not imported by this file; this is an error in Swift 6}}
74+
// expected-note@+1 {{The missing import of module 'Original' will be added implicitly}}
7275
public func takesGeneric<T: ProtoAlias>(_ t: T) {}
7376

7477
public struct HasMembers {
75-
// expected-warning@+1 {{'WrapperAlias' aliases 'Original.Wrapper' and cannot be used as property wrapper here because 'Original' was not imported by this file; this is an error in Swift 6}}
78+
// expected-warning@+2 {{'WrapperAlias' aliases 'Original.Wrapper' and cannot be used as property wrapper here because 'Original' was not imported by this file; this is an error in Swift 6}}
79+
// expected-note@+1 {{The missing import of module 'Original' will be added implicitly}}
7680
@WrapperAlias public var wrapped: Int
7781
}
7882

79-
// expected-warning@+1 {{'StructAlias' aliases 'Original.Struct' and cannot be used in an extension with public or '@usableFromInline' members because 'Original' was not imported by this file; this is an error in Swift 6}}
83+
// expected-warning@+2 {{'StructAlias' aliases 'Original.Struct' and cannot be used in an extension with public or '@usableFromInline' members because 'Original' was not imported by this file; this is an error in Swift 6}}
84+
// expected-note@+1 {{The missing import of module 'Original' will be added implicitly}}
8085
extension StructAlias {
8186
public func someFunc() {}
8287
}

0 commit comments

Comments
 (0)