Skip to content

Commit 746cddc

Browse files
committed
Sema: Improve MemberImportVisibility diags for for-in loops.
Ensure that source locations are attached to the implicit calls to `makeIterator()` and `next()` for diagnostics. Partially resolves rdar://144535697.
1 parent 324fa23 commit 746cddc

File tree

3 files changed

+46
-8
lines changed

3 files changed

+46
-8
lines changed

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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 2 {{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+
//--- other.swift
19+
20+
import lib
21+
22+
func makeSequence() -> EmptySequence {
23+
return MySequence()
24+
}
25+
26+
//--- lib.swift
27+
28+
public struct EmptySequence: Sequence {
29+
public struct Iterator: IteratorProtocol {
30+
public mutating func next() -> Int? { nil }
31+
}
32+
33+
public func makeIterator() -> Iterator { Iterator() }
34+
public init() { }
35+
}

0 commit comments

Comments
 (0)