Skip to content
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

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

Open
swift-ci opened this issue Aug 25, 2021 · 13 comments · May be fixed by #68264
Open

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

swift-ci opened this issue Aug 25, 2021 · 13 comments · May be fixed by #68264
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation expressions Feature: expressions good first issue Good for newcomers optional chaining Feature → expressions: optional chaining swift 5.9 type checker Area → compiler: Semantic analysis type inference Feature: type inference

Comments

@swift-ci
Copy link
Collaborator

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?'
@typesanitizer
Copy link

@swift-ci create

@xedin
Copy link
Member

xedin commented Aug 30, 2021

elidy (JIRA User) I see that you are assigned for this one, here is a hint - in repairFailures for GenericArgument we could check whether previous element is an ApplyArgToParam and if so record a AllowArgumentMismatch fix.

@LucianoPAlmeida
Copy link
Collaborator

LucianoPAlmeida commented Sep 1, 2021

@xedin Just an addition, this should be also the case for contextual type as well, for example let _: Int? = stringClass?.string

@xedin
Copy link
Member

xedin commented Sep 1, 2021

Sure, if ApplyArgToParam -> AllowArgumentMismatch fix, if ContextualType -> IgnoreContextualMismatch fix.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@LucianoPAlmeida LucianoPAlmeida self-assigned this Jun 24, 2022
@pedromotita
Copy link

Hello @LucianoPAlmeida are you still working on this issue?

First time contributor here, and got interested in this one. Any insights you could give me?

@AnthonyLatsis AnthonyLatsis added swift 5.9 type inference Feature: type inference optional chaining Feature → expressions: optional chaining expressions Feature: expressions labels Apr 20, 2023
@AnthonyLatsis
Copy link
Collaborator

Reduced set of test cases:

struct S {
  var str: String
}

func test(s: S?) {
  let _: Int? = s?.str

  func f(_: Int?) {}
  f(s?.str)
}

Environment update: Swift version 5.9-dev (LLVM 91080e65df791d6, Swift c1d5118)

@LucianoPAlmeida
Copy link
Collaborator

Hello @LucianoPAlmeida are you still working on this issue?

I actually forgot about this one 😆
If you want to take it go for it =]

First time contributor here, and got interested in this one. Any insights you could give me?

I think is exactly what @xedin mentioned in previous comments.

@Rajveer100
Copy link
Contributor

Rajveer100 commented Aug 30, 2023

elidy (JIRA User) I see that you are assigned for this one, here is a hint - in repairFailures for GenericArgument we could check whether previous element is an ApplyArgToParam and if so record a AllowArgumentMismatch fix.

@xedin
I suppose you meant the diagnoseFailureFor(...) which is where the type_of_expression_is_ambiguous is called.
If that's the case, we probably check for the condition as you mentioned earlier here?

@xedin
Copy link
Member

xedin commented Aug 30, 2023

No, we need this diagnosed in repairFailures, diagnoseFailureFor contains only fallback logic for cases were a fix hasn't been emitted.

@Rajveer100
Copy link
Contributor

Also, if I understand correctly, the last two elements of path refer to the fromType and toType, and they need to exist in order to call the diagnostic, or precisely add to conversionsOrFixes?

@xedin
Copy link
Member

xedin commented Aug 31, 2023

Yes, the fixes are produced based on the failing constraint, repairFailures gets the types, the constraint and its location passed by the caller, repairFailures checks what does the location (identified by a locator) represent and based on that emits an appropriate fix, you can take a look at a few other case statements in repairFailures to better understand what is going on there, some of them a quite small.

@LucianoPAlmeida
Copy link
Collaborator

@pedromotita Are you still working on this? @Rajveer100 started a PR, so if not actively working on it can we change the assignee for this issue?

Rajveer100 added a commit to Rajveer100/swift that referenced this issue Sep 1, 2023
@pedromotita pedromotita removed their assignment Sep 1, 2023
@pedromotita
Copy link

Already removed myself as the assignee. Keep on keeping on @Rajveer100 👍 😉

Rajveer100 added a commit to Rajveer100/swift that referenced this issue Sep 1, 2023
Rajveer100 added a commit to Rajveer100/swift that referenced this issue Sep 1, 2023
Rajveer100 added a commit to Rajveer100/swift that referenced this issue Sep 2, 2023
Rajveer100 added a commit to Rajveer100/swift that referenced this issue Sep 11, 2023
Rajveer100 added a commit to Rajveer100/swift that referenced this issue Oct 12, 2023
Rajveer100 added a commit to Rajveer100/swift that referenced this issue Oct 12, 2023
Rajveer100 added a commit to Rajveer100/swift that referenced this issue Nov 7, 2023
Rajveer100 added a commit to Rajveer100/swift that referenced this issue Nov 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation expressions Feature: expressions good first issue Good for newcomers optional chaining Feature → expressions: optional chaining swift 5.9 type checker Area → compiler: Semantic analysis type inference Feature: type inference
Projects
None yet
7 participants