Skip to content

Commit 6d24c52

Browse files
author
Gabor Horvath
committed
[cxx-interop] Use the locations imported from C++
A recent PR (#77204) started to import C++ source locations into Swift. This PR flips a switch so these locations are actually used more widely. Now some of the diagnostic locations are changed, but they generally improved the quality of the diagnostics, pointing out conformances imported from Obj-C code right when they are declared.
1 parent 09c89fb commit 6d24c52

File tree

13 files changed

+33
-36
lines changed

13 files changed

+33
-36
lines changed

lib/AST/Decl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,7 @@ static_assert(sizeof(checkSourceLocType(&ID##Decl::getLoc)) == 2, \
10111011
return getLocFromSource();
10121012
switch(File->getKind()) {
10131013
case FileUnitKind::Source:
1014+
case FileUnitKind::ClangModule:
10141015
return getLocFromSource();
10151016
case FileUnitKind::SerializedAST: {
10161017
if (!SerializedOK)
@@ -1019,7 +1020,6 @@ static_assert(sizeof(checkSourceLocType(&ID##Decl::getLoc)) == 2, \
10191020
}
10201021
case FileUnitKind::Builtin:
10211022
case FileUnitKind::Synthesized:
1022-
case FileUnitKind::ClangModule:
10231023
case FileUnitKind::DWARFModule:
10241024
return SourceLoc();
10251025
}

lib/Sema/TypeCheckMacros.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,12 +1351,11 @@ static SourceFile *evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo,
13511351
if (!attrSourceFile)
13521352
return nullptr;
13531353

1354-
// If the declaration has no source location and comes from a Clang module,
1354+
// If the declaration comes from a Clang module,
13551355
// pretty-print the declaration and use that location.
13561356
SourceLoc attachedToLoc = attachedTo->getLoc();
13571357
bool isPrettyPrintedDecl = false;
1358-
if (attachedToLoc.isInvalid() &&
1359-
isa<ClangModuleUnit>(dc->getModuleScopeContext())) {
1358+
if (isa<ClangModuleUnit>(dc->getModuleScopeContext())) {
13601359
isPrettyPrintedDecl = true;
13611360
attachedToLoc = evaluateOrDefault(
13621361
ctx.evaluator, PrettyPrintDeclRequest{attachedTo}, SourceLoc());

test/ClangImporter/cfuncs_scope.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ func testLocalVsFileScope() {
88

99
theFunctionInQuestion()
1010
// CHECK: :[[@LINE-1]]:25: error: missing argument
11-
// CHECK: LocalVsFileScope.theFunctionInQuestion:1:{{[0-9]+}}: note:
12-
// This is not a wonderful test because it's relying on the diagnostic
13-
// engine's synthesis of fake declarations to figure out what module the
14-
// importer assigned the function to. But, well, that's what we get.
11+
// CHECK: LocalVsFileScope.h:{{[0-9]+}}:{{[0-9]+}}: note:
1512

1613
aFunctionInBase() // just make sure it's imported
1714
// CHECK-NOT: :[[@LINE-1]]:{{[0-9]+}}:

test/ClangImporter/diags-with-many-imports.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,4 @@ import Module100
108108
obsoleted()
109109
// CHECK: [[@LINE-1]]:1: error:
110110
// CHECK: explicitly marked unavailable here
111-
// CHECK: func obsoleted()
111+
// CHECK: void obsoleted() __attribute__((unavailable));

test/ClangImporter/experimental_diagnostics_no_noise.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ s.c = 5
6060
// CHECK-NEXT: ctypes.h:{{[0-9]+}}:{{[0-9]+}}: note: built-in type 'Complex' not supported
6161
// CHECK-NEXT: int _Complex c;
6262
// CHECK-NEXT: ^
63-
// CHECK-NEXT: ctypes.PartialImport.a:{{[0-9]+}}:{{[0-9]+}}: note: did you mean 'a'?
64-
// CHECK-NEXT: public var a: Int32
65-
// CHECK-NEXT: ^
66-
// CHECK-NEXT: ctypes.PartialImport.b:{{[0-9]+}}:{{[0-9]+}}: note: did you mean 'b'?
67-
// CHECK-NEXT: public var b: Int32
68-
// CHECK-NEXT: ^
63+
// CHECK-NEXT: ctypes.h:{{[0-9]+}}:{{[0-9]+}}: note: did you mean 'a'?
64+
// CHECK-NEXT: int a;
65+
// CHECK-NEXT: ^
66+
// CHECK-NEXT: ctypes.h:{{[0-9]+}}:{{[0-9]+}}: note: did you mean 'b'?
67+
// CHECK-NEXT: int b;
68+
// CHECK-NEXT: ^
6969

7070
// CHECK-NOT: note
7171
// CHECK-NOT: warning

test/ClangImporter/experimental_diagnostics_opt_out.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ _ = bar.methodReturningForwardDeclaredInterface()
1919
let s: PartialImport
2020
s.c = 5
2121
// CHECK: experimental_diagnostics_opt_out.swift:{{[0-9]+}}:{{[0-9]+}}: error: value of type 'PartialImport' has no member 'c'
22-
// CHECK: ctypes.PartialImport.a:{{[0-9]+}}:{{[0-9]+}}: note: did you mean 'a'?
23-
// CHECK: ctypes.PartialImport.b:{{[0-9]+}}:{{[0-9]+}}: note: did you mean 'b'?
22+
// CHECK: ctypes.h:{{[0-9]+}}:{{[0-9]+}}: note: did you mean 'a'?
23+
// CHECK: ctypes.h:{{[0-9]+}}:{{[0-9]+}}: note: did you mean 'b'?
2424
// CHECK-NOT: warning
2525
// CHECK-NOT: error
2626
// CHECK-NOT: note

test/ClangImporter/generic_compatibility_alias.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import ObjCIRExtras
1111

1212
func foo(_: SwiftConstrGenericNameAlias<String>) {
1313
// expected-error@-1 {{'SwiftConstrGenericNameAlias' requires that 'String' inherit from 'NSNumber'}}
14-
// expected-note@-2 {{requirement specified as 'T' : 'NSNumber' [with T = String]}}
14+
// TODO: validate node in imported Obj-C header.
1515
}
1616

1717
func faz(_: SwiftGenericNameAlias<Int>) {
1818
// expected-error@-1 {{'SwiftGenericNameAlias' requires that 'Int' be a class type}}
19-
// expected-note@-2 {{requirement specified as 'T' : 'AnyObject' [with T = Int]}}
19+
// TODO: validate node in imported Obj-C header.
2020
}
2121

2222
func bar(_: SwiftGenericNameAlias<NSNumber>) {} // Ok

test/ClangImporter/objc_bridging_generics.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ func testImportedTypeParamRequirements() {
8888
let _ = PettableContainer<Rock>()
8989
let _ = PettableContainer<Porcupine>() // expected-error{{type 'Porcupine' does not conform to protocol 'Pettable'}}
9090
let _ = PettableContainer<Cat>()
91-
let _ = AnimalContainer<Desk>() // expected-error{{'AnimalContainer' requires that 'Desk' inherit from 'Animal'}} expected-note{{requirement specified as 'T' : 'Animal' [with T = Desk]}}
92-
let _ = AnimalContainer<Rock>() // expected-error{{'AnimalContainer' requires that 'Rock' inherit from 'Animal'}} expected-note{{requirement specified as 'T' : 'Animal' [with T = Rock]}}
91+
let _ = AnimalContainer<Desk>() // expected-error{{'AnimalContainer' requires that 'Desk' inherit from 'Animal'}} // TODO: add test for note appearing in Obj-c header.
92+
let _ = AnimalContainer<Rock>() // expected-error{{'AnimalContainer' requires that 'Rock' inherit from 'Animal'}} // TODO: add test for note appearing in Obj-c header.
9393
let _ = AnimalContainer<Porcupine>()
9494
let _ = AnimalContainer<Cat>()
95-
let _ = PettableAnimalContainer<Desk>() // expected-error{{'PettableAnimalContainer' requires that 'Desk' inherit from 'Animal'}} expected-note{{requirement specified as 'T' : 'Animal' [with T = Desk]}}
96-
let _ = PettableAnimalContainer<Rock>() // expected-error{{'PettableAnimalContainer' requires that 'Rock' inherit from 'Animal'}} expected-note{{requirement specified as 'T' : 'Animal' [with T = Rock]}}
95+
let _ = PettableAnimalContainer<Desk>() // expected-error{{'PettableAnimalContainer' requires that 'Desk' inherit from 'Animal'}} // TODO: add test for note appearing in Obj-c header.
96+
let _ = PettableAnimalContainer<Rock>() // expected-error{{'PettableAnimalContainer' requires that 'Rock' inherit from 'Animal'}} // TODO: add test for note appearing in Obj-c header.
9797
let _ = PettableAnimalContainer<Porcupine>() // expected-error{{type 'Porcupine' does not conform to protocol 'Pettable'}}
9898
let _ = PettableAnimalContainer<Cat>()
9999
}

test/DebugInfo/test-foundation.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ class MyObject : NSObject {
2626
// directly.
2727
// IMPORT-CHECK-DAG: !DIImportedEntity(tag: DW_TAG_imported_module, {{.*}}entity: ![[OVERLAY]]
2828

29-
// ALLOCCTOR-CHECK: ![[F:.*]] = !DIFile(filename: "<compiler-generated>",
3029
// ALLOCCTOR-CHECK: distinct !DISubprogram(name: "init", linkageName: "$sSo7NSArrayCABycfC"
31-
// ALLOCCTOR-CHECK-SAME: file: ![[F]],
30+
// ALLOCCTOR-CHECK-SAME: file: ![[F:[0-9]+]],
31+
// ALLOCCTOR-CHECK: ![[F]] = !DIFile(filename: "{{.*}}/NSArray.h",
3232
@objc func foo(_ obj: MyObject) {
3333
return obj.foo(obj)
3434
}

test/Inputs/clang-importer-sdk/usr/include/ObjCConcurrency.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ typedef void ( ^ObjCErrorHandler )( NSError * _Nullable inError );
179179
- (void) myMethod:(NSInteger)value1 foo:(NSInteger)value2;
180180
@end
181181

182-
@interface GenericObject<T> : NSObject
182+
@interface GenericObject<T> : NSObject // expected-note {{generic class 'GenericObject' does not conform to the 'Sendable' protocol}}
183183
- (void)doSomethingWithCompletionHandler:(void (^)(T _Nullable_result, NSError * _Nullable))completionHandler;
184184
- (void)doAnotherThingWithCompletionHandler:(void (^)(GenericObject<T> *_Nullable))completionHandler;
185185
@end
@@ -218,7 +218,7 @@ MAIN_ACTOR MAIN_ACTOR __attribute__((__swift_attr__("@MainActor"))) @protocol Tr
218218

219219
SENDABLE @interface SendableClass : NSObject @end
220220

221-
NONSENDABLE @interface NonSendableClass : NSObject @end
221+
NONSENDABLE @interface NonSendableClass : NSObject @end // expected-note {{class 'NonSendableClass' does not conform to the 'Sendable' protocol}}
222222

223223
ASSUME_NONSENDABLE_BEGIN
224224

@@ -232,14 +232,14 @@ SENDABLE @protocol SendableProtocol @end
232232
typedef NS_ENUM(unsigned, SendableEnum) {
233233
SendableEnumFoo, SendableEnumBar
234234
};
235-
typedef NS_ENUM(unsigned, NonSendableEnum) {
235+
typedef NS_ENUM(unsigned, NonSendableEnum) { // expected-note {{enum 'NonSendableEnum' does not conform to the 'Sendable' protocol}}
236236
NonSendableEnumFoo, NonSendableEnumBar
237237
} NONSENDABLE;
238238

239239
typedef NS_OPTIONS(unsigned, SendableOptions) {
240240
SendableOptionsFoo = 1 << 0, SendableOptionsBar = 1 << 1
241241
};
242-
typedef NS_OPTIONS(unsigned, NonSendableOptions) {
242+
typedef NS_OPTIONS(unsigned, NonSendableOptions) { // expected-note {{struct 'NonSendableOptions' does not conform to the 'Sendable' protocol}}
243243
NonSendableOptionsFoo = 1 << 0, NonSendableOptionsBar = 1 << 1
244244
} NONSENDABLE;
245245

@@ -264,10 +264,10 @@ UI_ACTOR
264264
@end
265265

266266
typedef NSString *SendableStringEnum NS_STRING_ENUM;
267-
typedef NSString *NonSendableStringEnum NS_STRING_ENUM NONSENDABLE;
267+
typedef NSString *NonSendableStringEnum NS_STRING_ENUM NONSENDABLE; // expected-note {{struct 'NonSendableStringEnum' does not conform to the 'Sendable' protocol}}
268268

269269
typedef NSString *SendableStringStruct NS_EXTENSIBLE_STRING_ENUM;
270-
typedef NSString *NonSendableStringStruct NS_EXTENSIBLE_STRING_ENUM NONSENDABLE;
270+
typedef NSString *NonSendableStringStruct NS_EXTENSIBLE_STRING_ENUM NONSENDABLE; // expected-note {{struct 'NonSendableStringStruct' does not conform to the 'Sendable' protocol}}
271271

272272
SENDABLE
273273
typedef struct {

test/Interop/Cxx/class/nonescapable-errors.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public func noAnnotations() -> View {
4848
// CHECK-NOT: nonescapable.h:19
4949
f2(nil, nil)
5050
// CHECK: nonescapable.h:23:6: warning: the returned type 'View' is annotated as non-escapable; its lifetime dependencies must be annotated
51+
// CHECKL nonescapable.h:23:6: error: cannot infer lifetime dependence, no parameters found that are either ~Escapable or Escapable with a borrowing ownership
5152
g(nil)
5253
return View()
5354
}

test/SourceKit/CursorInfo/cursor_info.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ let strInterpolation = "This is a \(stringStr + "ing") interpolation"
368368
// CHECK13: <decl.function.free><syntaxtype.keyword>func</syntaxtype.keyword> <decl.name>testDefaultParam</decl.name>(<decl.var.parameter><decl.var.parameter.argument_label>arg1</decl.var.parameter.argument_label>: <decl.var.parameter.type><ref.struct usr="s:Si">Int</ref.struct></decl.var.parameter.type> = 0</decl.var.parameter>)</decl.function.free>
369369

370370
// RUN: %sourcekitd-test -req=cursor -pos=34:4 %s -- -F %S/../Inputs/libIDE-mock-sdk -I %t.tmp %s | %FileCheck -check-prefix=CHECK14 %s
371-
// CHECK14: source.lang.swift.ref.function.free ({{.*}}Foo.framework/Frameworks/FooSub.framework/Headers/FooSub.h:4:5-4:16)
371+
// CHECK14: source.lang.swift.ref.function.free ({{.*}}Foo.framework/Frameworks/FooSub.framework/Headers/FooSub.h:4:5-4:22)
372372
// CHECK14: fooSubFunc1
373373
// CHECK14: c:@F@fooSubFunc1
374374
// CHECK14: source.lang.objc

test/TypeDecoder/objc_classes.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ do {
8787
// DEMANGLE-DECL: $sSo7NSCacheCySo8NSNumberCSo8NSStringCG
8888
// DEMANGLE-DECL: $sSo26NSPropertyListWriteOptionsa
8989

90-
// CHECK-DECL: Foundation.(file).NSSet
91-
// CHECK-DECL: Foundation.(file).NSFastEnumeration
90+
// CHECK-DECL: Foundation.(file).NSSet@{{.*}}NSSet.h:{{[0-9]+}}:{{[0-9]+}}
91+
// CHECK-DECL: Foundation.(file).NSFastEnumeration@{{.*}}NSEnumerator.h:{{[0-9]+}}:{{[0-9]+}}
9292
// CHECK-DECL: objc_classes.(file).OurObjCProtocol
93-
// CHECK-DECL: Foundation.(file).NSCache
94-
// CHECK-DECL: Foundation.(file).PropertyListSerialization extension.WriteOptions
93+
// CHECK-DECL: Foundation.(file).NSCache@{{.*}}NSCache.h:{{[0-9]+}}:{{[0-9]+}}
94+
// CHECK-DECL: Foundation.(file).PropertyListSerialization extension.WriteOptions@{{.*}}NSPropertyList.h:{{[0-9]+}}:{{[0-9]+}}

0 commit comments

Comments
 (0)