Skip to content

[SR-3953] Unary sign operator on integer literal gives "ambiguous use of operator" error #46538

@swift-ci

Description

@swift-ci
Previous ID SR-3953
Radar None
Original Reporter chowell (JIRA User)
Type Bug
Environment

macOS 10.12.3, MacBook Pro (Retina, 15-inch, Mid 2014)
Swift version 3.1-dev (LLVM 3887fe8485, Clang f9a0f935a5, Swift 59037e9)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, TypeChecker
Assignee @moiseev
Priority Medium

md5: 9859694b560d4f584e18ea58a30be0d0

Issue Description:

Swift will produce "ambiguous use of operator" errors from trivial expressions of a unary sign operator on an integer literal. Similar expressions using floating-point literals are unaffected. This is not a recent bug; it's also present in the Swift 3.0.2 and Swift 2.3 releases.

Here are some examples. First, the trivial input of a unary + on an integer literal:

+1

gives the output:

<stdin>:1:1: error: ambiguous use of operator '+'
+1
^
Swift.+:1:20: note: found this candidate
prefix public func +(x: Float) -> Float
                   ^
Swift.+:1:20: note: found this candidate
prefix public func +(x: Double) -> Double
                   ^
Swift.+:1:20: note: found this candidate
prefix public func +(x: Float80) -> Float80
                   ^

While an integer literal has no predefined type, it defaults to Int in the absence of further information. Yet from examining the constraint solver's debugging output, the generic operator

public prefix func + <T : SignedNumber>(x: T) -> T

which should satisfy this, doesn't seem to be actually checked by the solver as a potential solution, even though the solver lists it among the potential overloads of unary +.

This is inconsistent with Swift's treatment of

+1.0

which gives an unambiguous result, as expected.

The input:

-1

doesn't have this problem, but only because the minus sign is treated as part of the literal. If we use parentheses to force the use of the unary - operator:

-(1)

Swift again gives "ambiguous use" errors:

<stdin>:1:1: error: ambiguous use of operator '-'
-(1)
^
Swift.-:1:20: note: found this candidate
prefix public func -(x: Float) -> Float
                   ^
Swift.-:1:20: note: found this candidate
prefix public func -(x: Double) -> Double
                   ^
Swift.-:1:20: note: found this candidate
prefix public func -(x: Float80) -> Float80
                   ^

Again, the generic operators:

public prefix func - <T : SignedNumber>(x: T) -> T
public prefix func - <T: SignedArithmetic>(x: T) -> T

are apparently never checked by the constraint solver, even though it lists them among the overloads of unary -.

By constrast, the input:

-(1.0)

is unambiguous, as expected.

Incidentally, this bug appears to be "Case 1" in SR-2459, and I've found two references to what seems to be the same bug on Stack Overflow: one from over a year ago, and one from just this morning.

Metadata

Metadata

Assignees

Labels

bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselftype checkerArea → compiler: Semantic analysis

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions