Skip to content

Commit c5e5311

Browse files
authored
Merge pull request #80560 from tshortli/member-import-visibility-diags-implicit
Parse/Sema: Add source locations to implicit code for `MemberImportVisibility` diagnostics
2 parents ca1320e + 3e9db90 commit c5e5311

File tree

4 files changed

+161
-9
lines changed

4 files changed

+161
-9
lines changed

lib/Parse/ParseExpr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1972,7 +1972,7 @@ parseStringSegments(SmallVectorImpl<Lexer::StringSegment> &Segments,
19721972
new (Context) UnresolvedDotExpr(InterpolationVarRef,
19731973
/*dotloc=*/SourceLoc(),
19741974
appendLiteral,
1975-
/*nameloc=*/DeclNameLoc(),
1975+
/*nameloc=*/DeclNameLoc(TokenLoc),
19761976
/*Implicit=*/true);
19771977
auto *ArgList = ArgumentList::forImplicitUnlabeled(Context, {Literal});
19781978
auto AppendLiteralCall =

lib/Sema/CSGen.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4602,8 +4602,9 @@ generateForEachStmtConstraints(ConstraintSystem &cs, DeclContext *dc,
46024602
FuncDecl *makeIterator = isAsync ? ctx.getAsyncSequenceMakeAsyncIterator()
46034603
: ctx.getSequenceMakeIterator();
46044604

4605-
auto *makeIteratorRef = UnresolvedDotExpr::createImplicit(
4606-
ctx, sequenceExpr, makeIterator->getName());
4605+
auto *makeIteratorRef = new (ctx) UnresolvedDotExpr(
4606+
sequenceExpr, SourceLoc(), DeclNameRef(makeIterator->getName()),
4607+
DeclNameLoc(stmt->getForLoc()), /*implicit=*/true);
46074608
makeIteratorRef->setFunctionRefInfo(FunctionRefInfo::singleBaseNameApply());
46084609

46094610
Expr *makeIteratorCall =
@@ -4666,11 +4667,13 @@ generateForEachStmtConstraints(ConstraintSystem &cs, DeclContext *dc,
46664667
TinyPtrVector<Identifier> labels;
46674668
if (nextFn && nextFn->getParameters()->size() == 1)
46684669
labels.push_back(ctx.Id_isolation);
4669-
auto *nextRef = UnresolvedDotExpr::createImplicit(
4670-
ctx,
4670+
auto *makeIteratorVarRef =
46714671
new (ctx) DeclRefExpr(makeIteratorVar, DeclNameLoc(stmt->getForLoc()),
4672-
/*Implicit=*/true),
4673-
nextId, labels);
4672+
/*Implicit=*/true);
4673+
auto *nextRef = new (ctx)
4674+
UnresolvedDotExpr(makeIteratorVarRef, SourceLoc(),
4675+
DeclNameRef(DeclName(ctx, nextId, labels)),
4676+
DeclNameLoc(stmt->getForLoc()), /*implicit=*/true);
46744677
nextRef->setFunctionRefInfo(FunctionRefInfo::singleBaseNameApply());
46754678

46764679
ArgumentList *nextArgs;

test/AutoDiff/SILOptimizer/differentiation_control_flow_diagnostics.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,11 @@ enum Tree : Differentiable & AdditiveArithmetic {
168168
// (`Collection.makeIterator` and `IteratorProtocol.next`).
169169
// expected-error @+1 {{function is not differentiable}}
170170
@differentiable(reverse)
171-
// expected-note @+2 {{when differentiating this function definition}}
172-
// expected-note @+1 {{cannot differentiate through a non-differentiable result; do you want to use 'withoutDerivative(at:)'?}} {{+2:12-12=withoutDerivative(at: }} {{+2:17-17=)}}
173171
func loop_array(_ array: [Float]) -> Float {
172+
// expected-note@-1 {{when differentiating this function definition}}
174173
var result: Float = 1
175174
for x in array {
175+
// expected-note@-1 {{cannot differentiate through a non-differentiable result; do you want to use 'withoutDerivative(at:)'}}
176176
result = result * x
177177
}
178178
return result
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: split-file %s %t
3+
4+
// RUN: %target-swift-frontend -emit-module -o %t %t/lib.swift
5+
// RUN: %target-swift-frontend -typecheck -primary-file %t/main.swift %t/other.swift -I %t -verify -enable-upcoming-feature MemberImportVisibility
6+
7+
// REQUIRES: swift_feature_MemberImportVisibility
8+
9+
//--- main.swift
10+
11+
import Swift
12+
// expected-note 15 {{add import of module 'lib'}}
13+
14+
for _ in makeSequence() { }
15+
// expected-error@-1 {{instance method 'makeIterator()' is not available due to missing import of defining module 'lib'}}
16+
// expected-error@-2 {{instance method 'next()' is not available due to missing import of defining module 'lib'}}
17+
18+
takesNilExpressible(nil)
19+
// expected-error@-1 {{initializer 'init(nilLiteral:)' is not available due to missing import of defining module 'lib'}}
20+
21+
takesIntExpressible(1)
22+
// expected-error@-1 {{initializer 'init(integerLiteral:)' is not available due to missing import of defining module 'lib'}}
23+
24+
takesFloatExpressible(1.0)
25+
// expected-error@-1 {{initializer 'init(floatLiteral:)' is not available due to missing import of defining module 'lib'}}
26+
27+
takesBoolExpressible(true)
28+
// expected-error@-1 {{initializer 'init(booleanLiteral:)' is not available due to missing import of defining module 'lib'}}
29+
30+
takesUnicodeScalarExpressible("🐦")
31+
// expected-error@-1 {{initializer 'init(unicodeScalarLiteral:)' is not available due to missing import of defining module 'lib'}}
32+
33+
takesExtendedGraphemeClusterExpressible("🦸🏾‍♀️")
34+
// expected-error@-1 {{initializer 'init(extendedGraphemeClusterLiteral:)' is not available due to missing import of defining module 'lib'}}
35+
36+
takesStringLiteralExpressible("Hello world")
37+
// expected-error@-1 {{initializer 'init(stringLiteral:)' is not available due to missing import of defining module 'lib'}}
38+
39+
takesArrayExpressible([1])
40+
// expected-error@-1 {{initializer 'init(arrayLiteral:)' is not available due to missing import of defining module 'lib'}}
41+
42+
takesDictionaryExpressible(["one": 1])
43+
// expected-error@-1 {{initializer 'init(dictionaryLiteral:)' is not available due to missing import of defining module 'lib'}}
44+
45+
takesMessage("\(1)")
46+
// expected-error@-1 {{initializer 'init(stringInterpolation:)' is not available due to missing import of defining module 'lib'}}
47+
// expected-error@-2 {{instance method 'appendInterpolation' is not available due to missing import of defining module 'lib'}}
48+
// expected-error@-3 2 {{instance method 'appendLiteral' is not available due to missing import of defining module 'lib'}}
49+
50+
takesColorExpressible(#colorLiteral(red: 0.0, green: 0.0, blue: 0.0, alpha: 1))
51+
// FIXME: Missing diangostic
52+
53+
takesImageExpressible(#imageLiteral(resourceName: "image.png"))
54+
// FIXME: Missing diangostic
55+
56+
takesFileReferenceExpressible(#fileLiteral(resourceName: "file.txt"))
57+
// FIXME: Missing diangostic
58+
59+
//--- other.swift
60+
61+
import lib
62+
63+
func makeSequence() -> EmptySequence {
64+
return MySequence()
65+
}
66+
67+
func takesNilExpressible(_ x: NilExpressible) { }
68+
func takesIntExpressible(_ x: IntExpressible) { }
69+
func takesFloatExpressible(_ x: FloatExpressible) { }
70+
func takesBoolExpressible(_ x: BoolExpressible) { }
71+
func takesUnicodeScalarExpressible(_ x: UnicodeScalarExpressible) { }
72+
func takesExtendedGraphemeClusterExpressible(_ x: ExtendedGraphemeClusterExpressible) { }
73+
func takesStringLiteralExpressible(_ x: StringExpressible) { }
74+
func takesArrayExpressible<E>(_ x: ArrayExpressible<E>) { }
75+
func takesDictionaryExpressible<K, V>(_ x: DictionaryExpressible<K, V>) { }
76+
func takesMessage(_ x: Message) { }
77+
func takesColorExpressible(_ x: ColorExpressible) { }
78+
func takesImageExpressible(_ x: ImageExpressible) { }
79+
func takesFileReferenceExpressible(_ x: FileReferenceExpressible) { }
80+
81+
//--- lib.swift
82+
83+
public struct EmptySequence: Sequence {
84+
public struct Iterator: IteratorProtocol {
85+
public mutating func next() -> Int? { nil }
86+
}
87+
88+
public func makeIterator() -> Iterator { Iterator() }
89+
public init() { }
90+
}
91+
92+
public struct NilExpressible: ExpressibleByNilLiteral {
93+
public init(nilLiteral: ()) { }
94+
}
95+
96+
public struct IntExpressible: ExpressibleByIntegerLiteral {
97+
public init(integerLiteral value: Int) { }
98+
}
99+
100+
public struct FloatExpressible: ExpressibleByFloatLiteral {
101+
public init(floatLiteral value: Float) { }
102+
}
103+
104+
public struct BoolExpressible: ExpressibleByBooleanLiteral {
105+
public init(booleanLiteral value: Bool) { }
106+
}
107+
108+
public struct UnicodeScalarExpressible: ExpressibleByUnicodeScalarLiteral {
109+
public init(unicodeScalarLiteral value: Unicode.Scalar) { }
110+
}
111+
112+
public struct ExtendedGraphemeClusterExpressible: ExpressibleByExtendedGraphemeClusterLiteral {
113+
public init(extendedGraphemeClusterLiteral value: Character) { }
114+
}
115+
116+
public struct StringExpressible: ExpressibleByStringLiteral {
117+
public init(stringLiteral value: String) { }
118+
}
119+
120+
public struct ArrayExpressible<Element>: ExpressibleByArrayLiteral {
121+
public init(arrayLiteral elements: Element...) { }
122+
}
123+
124+
public struct DictionaryExpressible<Key, Value>: ExpressibleByDictionaryLiteral {
125+
public init(dictionaryLiteral elements: (Key, Value)...) { }
126+
}
127+
128+
public struct MessageInterpolation: StringInterpolationProtocol {
129+
public init(literalCapacity: Int, interpolationCount: Int) { }
130+
public mutating func appendInterpolation(_ value: @autoclosure () -> Int) { }
131+
public mutating func appendLiteral(_ literal: String) { }
132+
}
133+
134+
public struct Message: ExpressibleByStringInterpolation {
135+
public init(stringInterpolation: MessageInterpolation) { }
136+
public init(stringLiteral: String) { }
137+
}
138+
139+
public struct ColorExpressible: _ExpressibleByColorLiteral {
140+
public init(_colorLiteralRed red: Float, green: Float, blue: Float, alpha: Float) { }
141+
}
142+
143+
public struct ImageExpressible: _ExpressibleByImageLiteral {
144+
public init(imageLiteralResourceName path: String) { }
145+
}
146+
147+
public struct FileReferenceExpressible: _ExpressibleByFileReferenceLiteral {
148+
public init(fileReferenceLiteralResourceName path: String) { }
149+
}

0 commit comments

Comments
 (0)