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-10963] Error message from early return in convenience init could be improved #53351

Open
swift-ci opened this issue Jun 18, 2019 · 1 comment
Labels
compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation improvement

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-10963
Radar None
Original Reporter ajo (JIRA User)
Type Improvement
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Improvement, DiagnosticsQoI
Assignee ravikandhadai (JIRA)
Priority Medium

md5: dee45a3dee7ad1067a9f513cabcb0d44

Issue Description:

The mistake in the code below is difficult to find with the help of the compiler error:

> ./ConvenienceInit.swift:40:5: error: 'self' used before 'self.init' call or assignment to 'self'

The error points to the end of the convenience init, instead of the early return statement.

#!/usr/bin/swift

import Foundation

/*
 Xcode 10.2.1
 Apple Swift version 5.0.1 (swiftlang-1001.0.82.4 clang-1001.0.46.5)
 Target: x86_64-apple-darwin18.6.0

 ./ConvenienceInit.swift:36:13: warning: code after 'return' will never be executed
 return nil
 ^
 ./ConvenienceInit.swift:40:5: error: 'self' used before 'self.init' call or assignment to 'self'
 }
 
 ----
 
 Xcode 11.0 beta 2
 Apple Swift version 5.1 (swiftlang-1100.0.43.3 clang-1100.0.26.3)
 Target: x86_64-apple-darwin18.6.0

 ./ConvenienceInit.swift:36:13: warning: code after 'return' will never be executed
 return nil
 ^
 ./ConvenienceInit.swift:40:5: error: 'self' used before 'self.init' call or assignment to 'self'
 }

 */

class Class {
    let variable: Int
    
    convenience init?(withOptional argument: Int?) {
        guard let value = argument else {
            print("Unexpected: \(String(describing: argument))"); return
            return nil
        }
        
        self.init(with: value)
    }
    
    init(with argument: Int) {
        variable = argument
    }
}

let c = Class(withOptional: nil)

My suggestion here is to include the line number of any/the first path which contains an invalid return-statement (since there may well be many - and one of them, at a time, gives something to work with).

@belkadan
Copy link
Contributor

cc ravikandhadai (JIRA User) - not exactly what you're working on, but still SIL diagnostics.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation improvement
Projects
None yet
Development

No branches or pull requests

2 participants