Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent operators precedence #9169

Closed
bpineau opened this issue Oct 2, 2016 · 7 comments · Fixed by hashicorp/hil#33
Closed

Inconsistent operators precedence #9169

bpineau opened this issue Oct 2, 2016 · 7 comments · Fixed by hashicorp/hil#33

Comments

@bpineau
Copy link
Contributor

bpineau commented Oct 2, 2016

Hi,

Terraform Version

Terraform v0.7.4

Terraform Configuration Files

data "template_file" "example" {
  template = <<EOF

var1 = ${var1}
var2 = ${var2}
EOF

  vars {
    var1 = "${1 + 3 % 2}"
    var2 = "${3 % 2 + 1}"
  }
}

output "rendered" {
  value = "${data.template_file.example.rendered}"
}

Debug Output

rendered =
var1 = 0
var2 = 2

Expected Behavior

If "%" has precedence over "+", var1 should output "2"
If "+" has precedence over "%", var2 should output "0"

Actual Behavior

In the "1 + 3 % 2" case, the addition takes precedence (outputs 0).
In the "3 % 2 + 1" case, the modulo takes precedence (outputs 2)

Same thing with a simple division ("${2 + 2 / 2}" => 2 , "${2 / 2 + 2}" => 3).

As if the values were computed left to right, without precedence considerations ?
If that so, maybe should this be stated in the interpolation doc's "Math" section (since it could be counter-intuitive) ?

Steps to Reproduce

  1. terraform apply
@rottenbytes
Copy link

upvote for this one. even if using parenthesis allows to force order, this is pretty annoying

@mitchellh
Copy link
Contributor

The precedence isn't inconsistent, they're equal: + is equal to % currently. This is primarily due to legacy reasons and changing this now would actually likely break a large number of configurations.

We can mark this for 0.8 since it'll break configurations. :(

@mitchellh mitchellh added this to the Terraform 0.8 milestone Oct 25, 2016
@rottenbytes
Copy link

Hi @mitchellh,

I had the behaviour with - and * operators.

Example : in var.myvar * 1024.0 - var.myvar * 1024.0 * 0.9 : I had to use parenthesis and come up with (var.myvar * 1024.0) - (var.myvar * 1024.0 * 0.9) to get the expected result.

I think inconsistent is used here to mark what is usually expected from mathematic operator precedence.

@mitchellh
Copy link
Contributor

The expectation is fair and is why this issue is still open, but the fact is that this will still be a hugely backwards incompatible change and will likely break existing configs so we need to be careful about introducing it only in a major release, which we'll plan.

@rottenbytes
Copy link

I totally get that and understand not introducing breaking changes 🙇 Maybe add a hint in the docs as @bpineau stated ?

rottenbytes pushed a commit to rottenbytes/terraform that referenced this issue Oct 26, 2016
mitchellh added a commit that referenced this issue Oct 27, 2016
Add a note about operator precedence. #9169
@mitchellh
Copy link
Contributor

Thanks @rottenbytes for the docs hint. I'm going to close this for now since its an HIL bug that we have over there. We'll address this perhaps in 0.9. It is easy enough to do but I want to give people some warning/time before the switch. Thanks!

@ghost
Copy link

ghost commented Apr 21, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@hashicorp hashicorp locked and limited conversation to collaborators Apr 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants