[SR-15111] “Type of expression is ambiguous” error when initializing a class

Open
#57,437 13 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
32/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
swift
Domain
compilers

Research direction

Run the provided Swift example in the stated Xcode environment and compare the diagnostics for the direct optional-member argument and the intermediate variable. Investigate the compiler type-checker behavior described by the Compiler and TypeChecker labels; done means the direct call reports the same type-mismatch diagnostic as the second call.

Written by the indexing model from the issue text.

Description

bug compiler diagnostics quality expressions good first issue optional chaining swift 5.9 type checker type inference
Previous ID SR-15111
Radar rdar://problem/82407752
Original Reporter elidy (JIRA User)
Type Bug
Environment

Xcode Version 13.0 beta 4 (13A5201i)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, DiagnosticsQoI, StarterBug, TypeChecker
Assignee elidy (JIRA)
Priority Medium

md5: 37c1160eb7b2424ea42b3ec849906fef

Issue Description:

When a class is initialized, passing an argument as a property of an optional class instance of incorrect type results in “Type of expression is ambiguous” error.

 class IntClass {
    var int: Int?
    
    init(int: Int?) {
        self.int = int
    }
}

class StringClass {
    var string: String? = "hello"
}

var stringClass: StringClass? = StringClass()

IntClass(int: stringClass?.string)
//Type of expression is ambiguous without more context
//Expected error: Cannot convert value of type 'String?' to expected argument type 'Int?'

var stringValue = stringClass?.string
IntClass(int: stringValue)
//Cannot convert value of type 'String?' to expected argument type 'Int?'

Dominant language
Swift
Stars
70.4k
Forks
10.8k
Avg merge
2d 2h
Merged PRs (30d)
461

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from swiftlang/swift

All issues in swiftlang/swift

Similar issues

More Swift issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.