Skip to content

[CS] Correctly set compound bit for UnresolvedMemberExprs #77944

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 1 addition & 12 deletions include/swift/AST/Expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -1898,18 +1898,7 @@ class UnresolvedMemberExpr final
bool implicit)
: Expr(ExprKind::UnresolvedMember, implicit), DotLoc(dotLoc),
NameLoc(nameLoc), Name(name) {
// FIXME(FunctionRefInfo): Really, we should be passing `nameLoc` directly,
// allowing the FunctionRefInfo to be treated as compound. This would
// require us to enable IUOs for compound names, e.g:
// ```
// struct S {
// static func makeS(_: Int) -> S! { S() }
// }
//
// let s: S = .makeS(_:)(0)
// ```
setFunctionRefInfo(
FunctionRefInfo::unapplied(DeclNameLoc(nameLoc.getBaseNameLoc())));
setFunctionRefInfo(FunctionRefInfo::unapplied(nameLoc));
}

DeclNameRef getName() const { return Name; }
Expand Down
5 changes: 0 additions & 5 deletions lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1955,11 +1955,6 @@ OverloadChoice::getIUOReferenceKind(ConstraintSystem &cs,
auto refKind = getFunctionRefInfo();
assert(!forSecondApplication || refKind.isDoubleApply());

// Compound references currently never produce IUOs.
// FIXME(FunctionRefInfo): They should.
if (refKind.isCompoundName())
return std::nullopt;

switch (refKind.getApplyLevel()) {
case FunctionRefInfo::ApplyLevel::Unapplied:
// Such references never produce IUOs.
Expand Down
23 changes: 23 additions & 0 deletions test/Constraints/iuo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,26 @@ let _: Int? = returnsIUOFn()()
let _: Int = returnsIUOFn()() // expected-error {{value of optional type 'Int?' must be unwrapped to a value of type 'Int'}}
// expected-note@-1 {{coalesce using '??' to provide a default when the optional value contains 'nil'}}
// expected-note@-2 {{force-unwrap using '!' to abort execution if the optional value contains 'nil'}}

// Make sure it works for compound function references.
func testCompoundRefs() {
func hasArgLabel(x: Int) -> Int! { x }
struct S {
func hasArgLabel(x: Int) -> Int! { x }
}
let _ = hasArgLabel(x:)(0)
let _: Int? = hasArgLabel(x:)(0)
let _: Int = hasArgLabel(x:)(0)

let _ = S.hasArgLabel(x:)(S())(0)
let _: Int? = S.hasArgLabel(x:)(S())(0)
let _: Int = S.hasArgLabel(x:)(S())(0)

let _ = S().hasArgLabel(x:)(0)
let _: Int? = S().hasArgLabel(x:)(0)
let _: Int = S().hasArgLabel(x:)(0)

// We still don't allow IUOs for the function itself.
let _: (Int) -> Int = hasArgLabel(x:) // expected-error {{cannot convert value of type '(Int) -> Int?' to specified type '(Int) -> Int}}
let _: (Int) -> Int? = hasArgLabel(x:)
}
10 changes: 10 additions & 0 deletions test/Constraints/members.swift
Original file line number Diff line number Diff line change
Expand Up @@ -837,3 +837,13 @@ do {
// expected-note@-2 {{cast 'Any' to 'AnyObject' or use 'as!' to force downcast to a more specific type to access members}}
}
}

func testCompoundLeadingDot() {
struct S {
static func foo(x: Int) -> Self { .init() }
}

// Make sure we correctly strip the argument label.
let _: S = .foo(x:)(0)
let _: S = .foo(x:)(x: 0) // expected-error {{extraneous argument label 'x:' in call}}
}
2 changes: 1 addition & 1 deletion test/expr/primary/unqualified_name.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ struct S0 {
}

// Determine context from type.
let s0_static: S0 = .f3(_:y:z:)(0, y: 0, z: 0)
let s0_static: S0 = .f3(_:y:z:)(0, 0, 0)

class C0 {
init(x: Int, y: Int, z: Int) { }
Expand Down