Skip to content

Missing fix-it when opaque property type is inferred #69241

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

Open
natecook1000 opened this issue Oct 18, 2023 · 6 comments · May be fixed by #74811
Open

Missing fix-it when opaque property type is inferred #69241

natecook1000 opened this issue Oct 18, 2023 · 6 comments · May be fixed by #74811
Assignees
Labels
compiler The Swift compiler itself diagnostics QoI Bug: Diagnostics Quality of Implementation fix-its Feature: diagnostic fix-its good first issue Good for newcomers improvement opaque result types Feature → types → opaque types: opaque result types opaque types Feature → types: opaque types swift 6.0 type checker Area → compiler: Semantic analysis

Comments

@natecook1000
Copy link
Member

Description
When the type of a variable or property is inferred as an opaque type, the compiler emits an error to that effect:

Property definition has inferred type 'some Proto', involving the 'some' return type of another declaration

Steps to reproduce
When compiling this code, the last line has the above error attached.:

protocol Proto {}
struct Concrete: Proto {}
func getProto() -> some Proto { Concrete() }

let value = getProto() // error

Expected behavior
Since the fix is straightforward and included in the error message, the compiler should offer a fixit that would rectify the problem:

let value: some Proto = getProto()

Environment

  • Swift compiler version info Apple Swift version 5.9 (swiftlang-5.9.0.128.108 clang-1500.0.40.1) Target: arm64-apple-macosx14.0
@natecook1000 natecook1000 added improvement good first issue Good for newcomers fix-its Feature: diagnostic fix-its triage needed This issue needs more specific labels labels Oct 18, 2023
@AnthonyLatsis AnthonyLatsis added compiler The Swift compiler itself diagnostics QoI Bug: Diagnostics Quality of Implementation type checker Area → compiler: Semantic analysis opaque types Feature → types: opaque types opaque result types Feature → types → opaque types: opaque result types labels Oct 18, 2023
@AnthonyLatsis
Copy link
Collaborator

This compiles on the main branch. @hborla Is this a regression?

@AnthonyLatsis AnthonyLatsis removed the triage needed This issue needs more specific labels label Oct 18, 2023
@natecook1000
Copy link
Member Author

Gah, I thought I had tested this on my latest toolchain, but I had not. Verified working on this version of Swift:

Apple Swift version 5.9-dev (LLVM 2b5ff47e44b059c, Swift f177be06ab5a206)
Target: arm64-apple-macosx14.0

@vanvoorden
Copy link
Contributor

@natecook1000 I'm seeing a similar issue here… I'm trying to resolve a generic constraint using that opaque type. For some reason I seem to be breaking when I'm creating those variables inside the scope of a struct (but not when I'm creating those variables outside the struct). Any ideas or workarounds for that? Thanks!

protocol Proto {}
struct Concrete: Proto {}
func getProto() -> some Proto { Concrete() }

let value = getProto()

struct Wrapper<P: Proto> {
  let value: P
  
  init(_ wrappedValue: P) {
    self.value = wrappedValue
  }
  
  var wrappedValue: P {
    self.value
  }
}

let w1 = Wrapper(getProto())

let w2 = Wrapper(value)

struct S1 {
  let w1 = Wrapper(getProto())
  //  Property definition has inferred type 'Wrapper<some Proto>', involving the 'some' return type of another declaration
  let w2 = Wrapper(value)
  //  Property definition has inferred type 'Wrapper<some Proto>', involving the 'some' return type of another declaration
}

I tested this and saw the same error on the three latest toolchains:

  • Apple Swift version 5.9.2-dev (LLVM 2b42c5ce063a374, Swift 9067148bc9c9a72)
  • Apple Swift version 5.10-dev (LLVM 551823b7474ea05, Swift 3e816f55ab4b4d4)
  • Apple Swift version 5.11-dev (LLVM 79aab1b6aed58d0, Swift fd9726839309a33)

@vanvoorden
Copy link
Contributor

@natecook1000 @AnthonyLatsis I'm still unable to build that last example on 5.10 and 6.0. Are there any other open tasks for similar issues I can follow along and watch for? Thanks!

@AnthonyLatsis
Copy link
Collaborator

AnthonyLatsis commented May 17, 2024

Reopening with this test case:

func foo() -> some Equatable { 0 }

struct S1 {
  let w0 = foo()
}

Context: #73661 (comment)

@Androp0v
Copy link

Mind if I take a shot at this? 👀

Androp0v added a commit to Androp0v/swift that referenced this issue Jun 28, 2024
When a pattern binding at top level attempts to pick up another declaration's opaque result type as its type by type inference, provide a fixit to explicitly declare the type. This is done to prevent opaque result types from propagating nontrivially into other declarations' types, see 6db0540
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler The Swift compiler itself diagnostics QoI Bug: Diagnostics Quality of Implementation fix-its Feature: diagnostic fix-its good first issue Good for newcomers improvement opaque result types Feature → types → opaque types: opaque result types opaque types Feature → types: opaque types swift 6.0 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants