Skip to content

[SR-897] Misleading diagnostic when testing for nil in assertionFailure #43509

@swift-ci

Description

@swift-ci
Previous ID SR-897
Radar None
Original Reporter elia (JIRA User)
Type Improvement
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Improvement, DiagnosticsQoI
Assignee None
Priority Medium

md5: d23aee39609546cf931f8061ece93852

Issue Description:

While refactoring a piece of code, I transformed an unconditional `assertionFailure` in an `assert` but I forgot to change the function name. I got this piece of code, which produced a confusing diagnostic:

class A {}
let a: A? = nil

assertionFailure(a != nil, "error”)

Compiling with `swiftc` leads to:

DiagnosticBug.swift:5:20: error: value of type 'A?' can never be nil, comparison isn't allowed
assertionFailure(a != nil, "error")
                 ~ ^

A simple boolean comparison on the other hand produces the diagnostic you’d expect:

let b = true
assertionFailure(b == false, "error”)
DiagnosticBug.swift:9:20: error: cannot convert value of type 'Bool' to expected argument type 'String'
assertionFailure(b == false, "error")
                 ~~^~~~~~~~

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfdiagnostics QoIBug: Diagnostics Quality of Implementation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions