-- in.cue --
import "math"

full: {
	out: {
		env: {
			if true {
				PORT: math.Abs(out.port)
			}
		}
		if true {
			port: int | *30080
		}
	}
}
reduced: {
	if true {
		a: math.Abs(port)
	}
	port: int | *30080
}
-- secondary.cue --
related: {
	[string]: {
		if true {
			shared: all_total: foo.fooData.total
		}
	}
	foo: fooData: {
		total: tags: _hidden
		_hidden: extra: {}
	}
	#Tags: [string]: {}
	bar: {
		shared: {
			[string]: tags: #Tags
			bar_total: foo.fooData.total
		}
	}
}
-- let.cue --
issue3931: full: {
	bar: [string]: spec?: Spec
	if true {
		foo: label: "foo"
		bar: bar1: spec: selector: foo
	}
	let Spec = {
		selector?: Selector
	}
	let Selector = {
		label?: string
		expr?: [...string]
	}
}
-- issue3946.cue --
issue3946: reduced:{
	out: [for _, v in _inputs { v }]
	_inputs: [_]: {
		doExtra?: bool
		if doExtra != _|_ {
			if true {
				extra: "yes"
			}
		}
	}
	if true {
		_inputs: input1: name: "input1"
	}
}
-- out/eval/stats --
Leaks:  10
Freed:  51
Reused: 45
Allocs: 16
Retain: 6

Unifications: 57
Conjuncts:    86
Disjuncts:    67
-- out/evalalpha --
(struct){
  full: (struct){
    out: (struct){
      env: (struct){
        PORT: (int){ 30080 }
      }
      port: (int){ |(*(int){ 30080 }, (int){ int }) }
    }
  }
  reduced: (struct){
    a: (int){ 30080 }
    port: (int){ |(*(int){ 30080 }, (int){ int }) }
  }
  issue3946: (struct){
    reduced: (struct){
      out: (#list){
        0: (struct){
          name: (string){ "input1" }
          doExtra?: (bool){ bool }
        }
      }
      _inputs: (struct){
        input1: (struct){
          name: (string){ "input1" }
          doExtra?: (bool){ bool }
        }
      }
    }
  }
  issue3931: (struct){
    full: (struct){
      bar: (struct){
        bar1: (struct){
          spec: (struct){
            selector: (struct){
              label: (string){ "foo" }
              expr?: (list){
              }
            }
          }
        }
      }
      foo: (struct){
        label: (string){ "foo" }
      }
      let Spec#1 = (struct){
        selector?: (struct){
          label?: (string){ string }
          expr?: (list){
          }
        }
      }
      let Selector#2 = (struct){
        label?: (string){ string }
        expr?: (list){
        }
      }
    }
  }
  related: (struct){
    foo: (struct){
      fooData: (struct){
        total: (struct){
          tags: ~(related.foo.fooData._hidden)
        }
        _hidden: (struct){
          extra: (struct){
          }
        }
      }
      shared: (struct){
        all_total: ~(related.foo.fooData.total)
      }
    }
    #Tags: (#struct){
    }
    bar: (struct){
      shared: (struct){
        bar_total: (struct){
          tags: (#struct){
            extra: (#struct){
            }
          }
        }
        all_total: (struct){
          tags: (#struct){
            extra: (#struct){
            }
          }
        }
      }
    }
  }
}
-- diff/-out/evalalpha<==>+out/eval --
diff old new
--- old
+++ new
@@ -61,10 +61,7 @@
     foo: (struct){
       fooData: (struct){
         total: (struct){
-          tags: (struct){
-            extra: (struct){
-            }
-          }
+          tags: ~(related.foo.fooData._hidden)
         }
         _hidden: (struct){
           extra: (struct){
@@ -72,12 +69,7 @@
         }
       }
       shared: (struct){
-        all_total: (struct){
-          tags: (struct){
-            extra: (struct){
-            }
-          }
-        }
+        all_total: ~(related.foo.fooData.total)
       }
     }
     #Tags: (#struct){
-- out/eval --
(struct){
  full: (struct){
    out: (struct){
      env: (struct){
        PORT: (int){ 30080 }
      }
      port: (int){ |(*(int){ 30080 }, (int){ int }) }
    }
  }
  reduced: (struct){
    a: (int){ 30080 }
    port: (int){ |(*(int){ 30080 }, (int){ int }) }
  }
  issue3946: (struct){
    reduced: (struct){
      out: (#list){
        0: (struct){
          name: (string){ "input1" }
          doExtra?: (bool){ bool }
        }
      }
      _inputs: (struct){
        input1: (struct){
          name: (string){ "input1" }
          doExtra?: (bool){ bool }
        }
      }
    }
  }
  issue3931: (struct){
    full: (struct){
      bar: (struct){
        bar1: (struct){
          spec: (struct){
            selector: (struct){
              label: (string){ "foo" }
              expr?: (list){
              }
            }
          }
        }
      }
      foo: (struct){
        label: (string){ "foo" }
      }
      let Spec#1 = (struct){
        selector?: (struct){
          label?: (string){ string }
          expr?: (list){
          }
        }
      }
      let Selector#2 = (struct){
        label?: (string){ string }
        expr?: (list){
        }
      }
    }
  }
  related: (struct){
    foo: (struct){
      fooData: (struct){
        total: (struct){
          tags: (struct){
            extra: (struct){
            }
          }
        }
        _hidden: (struct){
          extra: (struct){
          }
        }
      }
      shared: (struct){
        all_total: (struct){
          tags: (struct){
            extra: (struct){
            }
          }
        }
      }
    }
    #Tags: (#struct){
    }
    bar: (struct){
      shared: (struct){
        bar_total: (struct){
          tags: (#struct){
            extra: (#struct){
            }
          }
        }
        all_total: (struct){
          tags: (#struct){
            extra: (#struct){
            }
          }
        }
      }
    }
  }
}
-- out/compile --
--- in.cue
{
  full: {
    out: {
      env: {
        if true {
          PORT: 〈import;math〉.Abs(〈3;out〉.port)
        }
      }
      if true {
        port: (int|*30080)
      }
    }
  }
  reduced: {
    if true {
      a: 〈import;math〉.Abs(〈1;port〉)
    }
    port: (int|*30080)
  }
}
--- issue3946.cue
{
  issue3946: {
    reduced: {
      out: [
        for _, v in 〈1;_inputs〉 {
          〈1;v〉
        },
      ]
      _inputs: {
        [_]: {
          doExtra?: bool
          if (〈0;doExtra〉 != _|_(explicit error (_|_ literal) in source)) {
            if true {
              extra: "yes"
            }
          }
        }
      }
      if true {
        _inputs: {
          input1: {
            name: "input1"
          }
        }
      }
    }
  }
}
--- let.cue
{
  issue3931: {
    full: {
      bar: {
        [string]: {
          spec?: 〈2;let Spec#1〉
        }
      }
      if true {
        foo: {
          label: "foo"
        }
        bar: {
          bar1: {
            spec: {
              selector: 〈3;foo〉
            }
          }
        }
      }
      let Spec#1 = {
        selector?: 〈1;let Selector#2〉
      }
      let Selector#2 = {
        label?: string
        expr?: [
          ...string,
        ]
      }
    }
  }
}
--- secondary.cue
{
  related: {
    [string]: {
      if true {
        shared: {
          all_total: 〈3;foo〉.fooData.total
        }
      }
    }
    foo: {
      fooData: {
        total: {
          tags: 〈1;_hidden〉
        }
        _hidden: {
          extra: {}
        }
      }
    }
    #Tags: {
      [string]: {}
    }
    bar: {
      shared: {
        [string]: {
          tags: 〈3;#Tags〉
        }
        bar_total: 〈2;foo〉.fooData.total
      }
    }
  }
}
