Skip to content

Commit ddfaf80

Browse files
authored
Merge pull request #37347 from xedin/rdar-77700261
[CSFix] Suppress ambiguity warning about non-`Optional` base with sta…
2 parents 63613a9 + a3fe65d commit ddfaf80

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

lib/Sema/CSFix.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1832,6 +1832,26 @@ SpecifyBaseTypeForOptionalUnresolvedMember::attempt(
18321832
if (memberDecl->isInstanceMember())
18331833
continue;
18341834

1835+
// Disable this warning for ambiguities related to a
1836+
// static member lookup in generic context because it's
1837+
// possible to declare a member with the same name on
1838+
// a concrete type and in an extension of a protocol
1839+
// that type conforms to e.g.:
1840+
//
1841+
// struct S : P { static var test: S { ... }
1842+
//
1843+
// extension P where Self == S { static var test: { ... } }
1844+
//
1845+
// And use that in an optional context e.g. passing `.test`
1846+
// to a parameter of expecting `S?`.
1847+
if (auto *extension =
1848+
dyn_cast<ExtensionDecl>(memberDecl->getDeclContext())) {
1849+
if (extension->getSelfProtocolDecl()) {
1850+
allOptionalBase = false;
1851+
break;
1852+
}
1853+
}
1854+
18351855
allOptionalBase &= bool(choice.getBaseType()
18361856
->getMetatypeInstanceType()
18371857
->getOptionalObjectType());

test/Constraints/static_members_on_protocol_in_generic_context.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,3 +299,20 @@ func test_fixit_with_where_clause() {
299299
func test_assoc<T: TestWithAssoc>(_: T) {}
300300
test_assoc(.intVar) // expected-error {{contextual member reference to static property 'intVar' requires 'Self' constraint in the protocol extension}}
301301
}
302+
303+
// rdar://77700261 - incorrect warning about assuming non-optional base for unresolved member lookup
304+
struct WithShadowedMember : P {}
305+
306+
extension WithShadowedMember {
307+
static var warnTest: WithShadowedMember { get { WithShadowedMember() } }
308+
}
309+
310+
extension P where Self == WithShadowedMember {
311+
static var warnTest: WithShadowedMember { get { fatalError() } }
312+
}
313+
314+
func test_no_warning_about_optional_base() {
315+
func test(_: WithShadowedMember?) {}
316+
317+
test(.warnTest) // Ok and no warning even though the `warnTest` name is shadowed
318+
}

0 commit comments

Comments
 (0)