Unrecognised operator + in my code (custom stdlib)

(Carl Peto) #1

I had a useable stdlib that just contained the implementation of Int, some pointers, Optional, Bool and other similar, low level things (no strings, arrays, etc.)

When I upgraded the compiler to 5.0 I'm getting weird errors in my test code compiled against this now. It's like it doesn't recognise + any more, despite the fact it's definitely in my Swift.swift (minimal stdlib) file and that has compiled to a module without error.

Even more weird, this code still shows the same error:

import Swift

infix operator +

extension Int {
  static func +(lhs: Int, rhs: Int) -> Int {
    return lhs

let i: Int = 3

while true {
  var j: Int = i + 9

I still get the error that the operator is not recognised. (I don't have the exact error here, I'll post it tonight.)

Anyone any ideas how could that be?

(Jordan Rose) #2

I suspect redeclaring the operator + in this file is throwing things off, especially with @rudkx's work in Pitch: Making expression type checking of operator expressions fast.

(Steve Canon) #3

Isn't @rudkx's thing staged and off by default in 5.0?

(Jordan Rose) #4

Oops, right. Just the first part, then.

(Carl Peto) #5

Thanks guys. :slight_smile:

Actually I got exactly the same error when I just had...

import Swift

let i: Int = 3

while true {
  var j: Int = i + 9

I only added the operator again in my file to try and figure out why it wasn't recognised. :-/

I also noticed that operators seem to have protocols they apply to now or something? So in stdlib the definition is something like:

infix operator + : AdditionPrecedence, AdditiveArithmetic, String, Array, Strideable

...what effect do the protocol names after the precedence specifier have? Could that be interfering with parsing the + operator correctly in my code somehow?

(p.s. In my version I'm just doing... infix operator + : AdditionPrecedence, AdditiveArithmetic ...because I don't have the other protocols defined.)

(Jordan Rose) #6

That's Mark's new work, but as Steve points out it's off by default, so I wouldn't expect it to affect your custom stdlib.

(Steve Canon) #7

Is your custom stdlib available somewhere that we could try building it and see the error ourselves? I can't guarantee exactly when I'll have time to do so, but if you can point me to it I'll try to take a look.

@jrose parsing Mark's thing is off by default too, right? So if @carlos42421 has a designated protocol in the operator declaration, either he's building with at least that piece enabled (and so maybe the other piece too?) or the operator declaration may not be getting parsed (but I would expect an error then?)

(Carl Peto) #8

Hi Steve,

Thank you!

I haven’t yet. It’s not really useable by anyone else in its current form. But I’ll throw it up on github hopefully tonight or on the weekend.

Any advice you can give is very much appreciated!


(Carl Peto) #9

@scanon - here's a link where I've uploaded what I'm working on: https://github.com/carlos4242/uSwift

Here's the error I'm seeing:

main.swift:1:8: warning: implicit import of bridging header 'AVR.h' via module 'AVR' is deprecated and will be removed in a later version of Swift
import AVR
main.swift:28:18: error: use of unresolved operator '+'
  var v: Int = i + 9
make: *** [main.ll] Error 1

(Ben Rimmington) #10

The +(_:_:) function is commented out in the AdditiveArithmetic protocol.

(Carl Peto) #11

I've got the + operator defined just for Int on line 1266...

static func +(lhs: Int, rhs: Int) -> Int {
  return lhs

So I thought that would do the trick?

Also I sort of expected a different error message in that case, like Int does not implement the + operator or something? Maybe what I'm not clear on is what does unresolved operator mean? Does it mean "there's no definition for that operator" or does it mean "I cannot find an overload for that operator that fits these type constraints in this expression" type of thing? Sorry if I'm asking dumb questions. :confused:

(Carl Peto) #12


public static func +(lhs: Int, rhs: Int) -> Int {
  return lhs

And now it's working. :smiley:

So it looks like the error message means something more like, "I know that + is an operator with this associativity and precedence, but I can't find an implementation that matches the type constraints in this expression".

Still, dumb mistake on my part!

But, I can work with this... thanks guys!

(Steve Canon) #13

A bug report to improve the error message would not be unwelcome.

(Carl Peto) #14

I’ll make sure it’s not just a side effect of my crazy stdlib first. Then raise one if it seems to be a general issue. Otherwise it’s just a problem in my odd little world. Ha. :slight_smile: