Skip to content

error: failed to produce diagnostic for expression #62519

@gaultier

Description

@gaultier

Description
The compiler fails to build a simple one file program and halts with this error.

Steps to reproduce

import Foundation

var input = try! String(contentsOffile: CommandLine.arguments[1], encoding: String.Encoding.utf8)
let lines = input.split(separator: "\n")

for (_, line) in lines.enumerated() {
    print(line)
}

Command:

$ swiftc program.swift
/tmp/bug.swift:3:18: error: no exact matches in call to initializer
var input = try! String(contentsOffile: CommandLine.arguments[1], encoding: String.Encoding.utf8)
                 ^
Foundation.String:14:12: note: incorrect labels for candidate (have: '(contentsOffile:encoding:)', expected: '(contentsOfFile:encoding:)')
    public init(contentsOfFile path: __shared String, encoding enc: String.Encoding) throws
           ^
Foundation.String:23:12: note: incorrect labels for candidate (have: '(contentsOffile:encoding:)', expected: '(cString:encoding:)')
    public init?(cString: String, encoding enc: String.Encoding)
           ^
/tmp/bug.swift:6:1: error: failed to produce diagnostic for expression; please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project
for (_, line) in lines.enumerated() {
^

Expected behavior

The source code is wrong since it should be contentsOfFile not contentsOffile as pointed out by the first error message, so that part is fine.

The issue is with the last error message.I am not sure why it gets printed, maybe nothing should be printed? Or unable to infer type for expression due to a previous error perhaps.

Environment

  • Swift compiler version info
swift-driver version: 1.62.15 Apple Swift version 5.7.1 (swiftlang-5.7.1.135.3 clang-1400.0.29.51)
Target: x86_64-apple-macosx12.0
  • Xcode version info
Xcode 14.1
Build version 14B47b
  • Deployment target: macOS 12.6.1

All done on the command line (no Xcode involved).

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 Implementationfailed to produce diagnosticBug → internal error: Failed to produce diagnostic for expressiontype checkerArea → compiler: Semantic analysis

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions