Consumption of Copyable value

There is a strange behaviour of the consume operator. I apply consume operator to a value of Copyable type (String to be specific) in three slightly different cases:

  • consume of a parameter
  • consume of a local variable
  • consume of a variable declared at the top-level of main.swift (perhaps treated as a global variable)
func testA(x: consuming String) { // โœ… Error: 'x' consumed more than once
  let y = consume x
  print(y)
  print(x)
}

func testB() {
  let x = "x" // โœ… Error: 'x' used after consume
  let y = consume x
  print(y)
  print(x)
}

// testC
let x = "x"
let y = consume x // โŒ Error: 'consume' applied to value that the compiler does not support. This is a compiler bug. Please file a bug with a small example of the bug
print(y)

I wonder if this should work and there is a bug in the compiler or this shouldn't and the error message is just needs to be refined.

1 Like

The poor error message is definitely a bug. As top-level code is currently implemented, script globals are actual global variables, so they can't be statically consumed, and it is correct for the compiler to reject the code, though.

5 Likes

Should I file an issue on GH or you will handle it?

If you have the time, it would definitely help if you do file an issue. Thanks!

No problem consume operator applied to a global var of a Copyable type produces an incorrect error message ยท Issue #67755 ยท apple/swift ยท GitHub

1 Like