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-13416] Poor diagnostic when referenced struct member doesn't exist #55857

Closed
swift-ci opened this issue Aug 19, 2020 · 9 comments
Closed
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 type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-13416
Radar rdar://problem/67451811
Original Reporter rmann (JIRA User)
Type Bug
Status Resolved
Resolution Done

Attachment: Download

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

md5: 9d0de4bd604a4ca7f9333bed281168aa

Issue Description:

I've written this Vapor 4 code (in a RouteCollection struct):

    private
    func
    getMachines(_ inReq: Request)
        throws
        -> EventLoopFuture<[MachineContent]>
    {
        let orgURLName = inReq.parameters.get("orgURLName")
        let userID = try inReq.auth.userID()
        let r: EventLoopFuture<[MachineContent]> = inReq.users
            .find(id: userID, prefetchOrganizations: true)
            .unwrap(or: AuthenticationError.userNotFound)
            .guard(
            {
                print("User \($0)")
                return $0.organizations.contains { $0.orgURLName == orgURLName }
            },
            else: ApplicationError.notFound)
            .flatMap
            { inUser -> EventLoopFuture<[MachineContent]> in
                return inReq.eventLoop.makeSucceededFuture([MachineContent]())
            }
        return r
    }

The error reported by Swift in Xcode 11.6 (11E708) is "Unable to infer closure type in the current context" with the highlighted section of code being the opening brace on "$0.orgURLName == orgURLName". The actual field name is "urlName", and correcting that in the above code allows it to compile.

The diagnostic message should be more like "No such member `orgURLName`, did you mean `urlName`" with the offending field name highlighted.

Removing the print() and "return" from the closure results in the same diagnostic message, but this time highlighting the opening brace of the guard(else🙂 argument. (when broken up among several lines; when all on one line, it highlights the whole closure).

One additional note: As I was experimenting with this, I changed "$0.urlName" to "$0.asdf" and it gave me a different error, something along the lines of "cannot assign to…". That made me think it was due to the shadowed name "orgURLName", but now I can't get that error to reproduce, regardless of the field name I use.

@typesanitizer
Copy link

@swift-ci create

@xedin
Copy link
Member

xedin commented Aug 24, 2020

rmann (JIRA User) Could you please try with the latest master snapshot to see whether this has been fixed already? We made some changes recently to address similar problems. Also it would be great if you could attach the project we could use to reproduce or minimize the example...

@swift-ci
Copy link
Collaborator Author

Comment by Rick M (JIRA)

@xedin I'm reluctant to add the project to a public DB, but if there's a private place I can put it, I will do that (Apple Radar?).

I don't think I can take the time to try it with master, unless it's easy to add to Xcode (it used to be, but I don't see how in Xcode 11).

@xedin
Copy link
Member

xedin commented Aug 27, 2020

rmann (JIRA User) It's pretty easy to add a snapshot from swift.org as a toolchain in Xcode. Installed would put it in the right plot so it could be selected in the Xcode -> Toolchains. Regarding private place - you can file a radar (via Feedback Assistant I think) and attach your sources there.

@swift-ci
Copy link
Collaborator Author

Comment by Rick M (JIRA)

When I look in Prefs->Components, I only have Simulators, no Toolchains, but the Help says it should be there. Maybe I need to install a toolchain first? BAD UI, APPLE >:o

So, swift-DEVELOPMENT-SNAPSHOT-2020-08-18-a-osx.pkg, swift-5.3-DEVELOPMENT-SNAPSHOT-2020-08-18-a-osx.pkg, or is there a more bleeding-edge toolchain I should try?

@xedin
Copy link
Member

xedin commented Aug 27, 2020

Please use `swift-DEVELOPMENT-SNAPSHOT-2020-08-18-a-osx.pkg` one which is a snapshot of trunk.

@swift-ci
Copy link
Collaborator Author

Comment by Rick M (JIRA)

My project is posted here: https://feedbackassistant.apple.com/feedback/8547655

I think you can just DL the dependencies and try to build it. The file in question is "OrganizationsController.swift", the method is getMachinesErr().

@swift-ci
Copy link
Collaborator Author

Comment by Rick M (JIRA)

I can't come anywhere near building my project with swift-DEVELOPMENT-SNAPSHOT-2020-08-18-a-osx.pkg. This is after cleaning the build folder and relaunching Xcode.

Build target FluentKit_2020-08-26T17-04-30.txt

@xedin
Copy link
Member

xedin commented Apr 3, 2021

I have used project from feedback and the error I get from the latest main branch snapshot is - `error: value of type 'Organization' has no member 'orgURLName’` which seems to be exactly what is expected. Please try using the latest snapshot to verify and close this issue.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
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 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

3 participants