Resolve constant assignments by scope and skip overwrites
A constant binds its name in the lexical scope and replacing an existing one changes it for every later reader, so the full name is checked against openvox's and every loaded file's constants, which one world now shares. X ||= v depends on what other inputs defined, and a value constant is not a class or module.
This commit is contained in:
@@ -443,3 +443,30 @@ func TestFidelityRegexLiterals(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// A constant assignment binds its name in the lexical scope and replaces any existing value for every later reader:
|
||||
// openvox 9.3.0 writes t.pp for each want case and fails to load the others, or loads them only by overwriting a
|
||||
// constant, which w10k does not model.
|
||||
func TestFidelityConstantScope(t *testing.T) {
|
||||
for name, c := range map[string]struct {
|
||||
files map[string]string
|
||||
want bool
|
||||
}{
|
||||
"new top-level Type": {map[string]string{"m/lib/puppet/type/t.rb": "Type = 1\n" + classic}, true},
|
||||
"new Puppet::Feature": {map[string]string{"m/lib/puppet/type/t.rb": "module Puppet\n Feature = 1\nend\n" + classic}, true},
|
||||
"new constant in a provider": {provider("Puppet::Type.type(:t).provide(:x) do\n Tempfile = 1\nend\n"), true},
|
||||
"defined only if undefined": {map[string]string{"m/lib/puppet/type/t.rb": "Y_W10K = 1\nY_W10K = 2 unless defined?(Y_W10K)\n" + classic}, true},
|
||||
"Puppet::Util overwritten": {map[string]string{"m/lib/puppet/type/t.rb": "module Puppet\n Util = 1\nend\n" + classic}, false},
|
||||
"type block in module Puppet": {map[string]string{"m/lib/puppet/type/t.rb": "module Puppet\n Type.newtype(:t) do\n newparam(:name) { isnamevar }\n" +
|
||||
" Parameter = 1\n end\nend\n"}, false},
|
||||
"constant reassigned": {map[string]string{"m/lib/puppet/type/t.rb": "X_W10K = 1\nX_W10K = 2\n" + classic}, false},
|
||||
"library constant reassigned": {map[string]string{"m/lib/puppet_x/m/k.rb": "K_W10K = 1\n", "m/lib/puppet/type/t.rb": classic, "m/lib/puppet/provider/t/x.rb": "require 'puppet_x/m/k'\nPuppet::Type.type(:t).provide(:x) do\n K_W10K = 2\nend\n"}, false},
|
||||
"value constant opened as class": {map[string]string{"m/lib/puppet/type/t.rb": "X_W10K = 1\nclass X_W10K\nend\n" + classic}, false},
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
if got := exists(filepath.Join(fidelityEnv(t, c.files), OutDir, "t.pp")); got != c.want {
|
||||
t.Fatalf("t.pp written = %v, want %v", got, c.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ import (
|
||||
const OutDir = ".resource_types"
|
||||
|
||||
// Version changes whenever generated output can change, so deploys rebuild typed environments.
|
||||
const Version = "16"
|
||||
const Version = "17"
|
||||
|
||||
var moduleName = regexp.MustCompile(`^[a-z][a-z0-9_]*$`)
|
||||
|
||||
|
||||
+11
-10
@@ -37,8 +37,9 @@ type world struct {
|
||||
// provFiles are the provider files by type; types are the types Puppet::Type.type returns, nil where none loads
|
||||
provFiles map[string][]string
|
||||
types map[string]*loadedType
|
||||
// features are the libs of each Puppet.features.add a loaded file ran
|
||||
// features are the libs of each Puppet.features.add a loaded file ran; consts are constant values by full name
|
||||
features map[string][]string
|
||||
consts map[string]rval
|
||||
}
|
||||
|
||||
// loadedType is a type newtype stored; provs is set once its providerloader.loadall finished.
|
||||
@@ -47,12 +48,13 @@ type loadedType struct {
|
||||
provs map[string]*provDecl
|
||||
}
|
||||
|
||||
// Constant kinds: module, class, a Puppet::Provider subclass, a Puppet::Provider::ParsedFile subclass.
|
||||
// Constant kinds: module, class, a Puppet::Provider subclass, a Puppet::Provider::ParsedFile subclass, any other value.
|
||||
const (
|
||||
kMod = 'm'
|
||||
kClass = 'c'
|
||||
kProv = 'p'
|
||||
kParsed = 'f'
|
||||
kVal = 'v'
|
||||
)
|
||||
|
||||
//go:embed startup.txt
|
||||
@@ -71,7 +73,7 @@ var startup = func() map[string]byte {
|
||||
}()
|
||||
|
||||
func newWorld(resolve Resolver) *world {
|
||||
w := &world{resolve: resolve, defined: map[string]bool{}, kind: map[string]byte{}, sup: map[string]string{}, user: map[string]bool{}, mods: map[string][]string{}, loaded: map[string]bool{}, classes: map[string]*classCtx{}, tclasses: map[string]bool{}, types: map[string]*loadedType{}, features: map[string][]string{}}
|
||||
w := &world{resolve: resolve, defined: map[string]bool{}, kind: map[string]byte{}, sup: map[string]string{}, user: map[string]bool{}, mods: map[string][]string{}, loaded: map[string]bool{}, classes: map[string]*classCtx{}, tclasses: map[string]bool{}, types: map[string]*loadedType{}, features: map[string][]string{}, consts: map[string]rval{}}
|
||||
for c, k := range startup {
|
||||
w.define(c, k)
|
||||
}
|
||||
@@ -212,7 +214,7 @@ func (p *parser) load(f libFile) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("lib/%s: %w", f.rel, err)
|
||||
}
|
||||
q := &parser{toks: toks, consts: map[string]rval{}, used: map[int]bool{}, w: p.w, defined: p.w.defined, mod: f.mod, dir: path.Dir(f.rel), file: path.Base(f.rel), depth: p.depth, src: f.src}
|
||||
q := &parser{toks: toks, consts: p.w.consts, used: map[int]bool{}, w: p.w, defined: p.w.defined, mod: f.mod, dir: path.Dir(f.rel), file: path.Base(f.rel), depth: p.depth, src: f.src}
|
||||
if err := q.body("", q.fileStmt(nil)); err != nil {
|
||||
return fmt.Errorf("lib/%s: %w", f.rel, q.wrap(err))
|
||||
}
|
||||
@@ -231,7 +233,7 @@ func (p *parser) fileStmt(prov func() (bool, error)) func() (bool, error) {
|
||||
return true, p.beginRequire()
|
||||
case t.kind == tIdent && (t.text == "module" || t.text == "class") && p.at(1).kind == tConst:
|
||||
return true, p.namespace()
|
||||
case t.kind == tConst && (p.assigns() || p.at(1).text == "||=") && !coreConsts[t.text]:
|
||||
case t.kind == tConst && (p.assigns() || p.at(1).text == "||="):
|
||||
return true, p.strictConstAssign()
|
||||
case t.kind == tGVar && t.text == "$LOAD_PATH" && p.at(1).text == "." && p.at(2).text == "unshift":
|
||||
return true, p.unshift()
|
||||
@@ -315,6 +317,8 @@ func (p *parser) namespace() error {
|
||||
}
|
||||
k, had := p.w.kind[name]
|
||||
switch {
|
||||
case k == kVal:
|
||||
return fmt.Errorf("%s is not a class or module", name)
|
||||
case !isClass:
|
||||
if had && k != kMod {
|
||||
return fmt.Errorf("%s is not a module", name)
|
||||
@@ -329,7 +333,7 @@ func (p *parser) namespace() error {
|
||||
}
|
||||
rs := p.lexical(sup)
|
||||
sk := p.w.kind[rs]
|
||||
if sk == kMod || sk == 0 {
|
||||
if sk == kMod || sk == kVal || sk == 0 {
|
||||
return fmt.Errorf("superclass %s is not a defined class", sup)
|
||||
}
|
||||
if had && p.w.sup[name] != rs {
|
||||
@@ -489,9 +493,6 @@ func (p *parser) classStmt(name string, c *classCtx) func() (bool, error) {
|
||||
p.scope = ""
|
||||
}
|
||||
defer func() { p.scope, p.cls = saved, savedCls }()
|
||||
if c.block && p.defined[t.text] {
|
||||
return true, fmt.Errorf("%s = overwrites a constant puppet defines", t.text)
|
||||
}
|
||||
return true, p.strictConstAssign()
|
||||
case p.assigns() && (t.kind == tIVar && !strings.HasPrefix(t.text, "@@") && (!providerIvars[t.text[1:]] || c.kind != kProv && c.kind != kParsed) ||
|
||||
t.kind == tGVar && plainGlobal.MatchString(t.text) && t.text != "$stdout" && t.text != "$stderr" && t.text != "$stdin"):
|
||||
@@ -1054,7 +1055,7 @@ func providerFile(f libFile, typ string, rsapi bool, w *world) (pr *provDecl, er
|
||||
}
|
||||
var name, parent, sup string
|
||||
var c *classCtx
|
||||
p := &parser{toks: toks, consts: map[string]rval{}, used: map[int]bool{}, w: w, defined: w.defined, mod: f.mod, dir: path.Dir(f.rel), file: path.Base(f.rel), src: f.src}
|
||||
p := &parser{toks: toks, consts: w.consts, used: map[int]bool{}, w: w, defined: w.defined, mod: f.mod, dir: path.Dir(f.rel), file: path.Base(f.rel), src: f.src}
|
||||
w.loaded[f.mod+"\x00"+f.rel] = true
|
||||
provide := func() (bool, error) {
|
||||
if !p.is(tConst, "Puppet") || p.at(1).text != "::" || p.at(2).text != "Type" || p.at(3).text != "." || p.at(4).text != "type" {
|
||||
|
||||
@@ -155,7 +155,7 @@ func parseFile(f libFile, w *world) (types []*rtype, err error) {
|
||||
return nil, err
|
||||
}
|
||||
w.loaded[f.mod+"\x00"+f.rel] = true
|
||||
p := &parser{toks: toks, consts: map[string]rval{}, used: map[int]bool{}, w: w, defined: w.defined, mod: f.mod, dir: path.Dir(f.rel), file: path.Base(f.rel), src: f.src}
|
||||
p := &parser{toks: toks, consts: w.consts, used: map[int]bool{}, w: w, defined: w.defined, mod: f.mod, dir: path.Dir(f.rel), file: path.Base(f.rel), src: f.src}
|
||||
if err := p.body("", p.topStmt(false)); err != nil {
|
||||
return nil, p.wrap(err)
|
||||
}
|
||||
@@ -415,10 +415,12 @@ func (p *parser) skip(block bool) error {
|
||||
}
|
||||
}
|
||||
|
||||
// constAssign evaluates a constant assignment, which binds the name in the lexical scope; a name any loaded code
|
||||
// already defines would be overwritten for every later reader, so it is an error.
|
||||
func (p *parser) constAssign() (bool, error) {
|
||||
save := p.i
|
||||
name := p.take().text
|
||||
p.take()
|
||||
orEq := p.take().text == "||="
|
||||
v, err := p.eval()
|
||||
assign := true
|
||||
if err == nil && (p.is(tIdent, "if") || p.is(tIdent, "unless")) {
|
||||
@@ -441,19 +443,30 @@ func (p *parser) constAssign() (bool, error) {
|
||||
if p.scope != "" {
|
||||
name = p.scope + "::" + name
|
||||
}
|
||||
if _, ok := p.consts[name]; ok || p.defined[name] {
|
||||
return true, fmt.Errorf("%s = overwrites a defined constant", name)
|
||||
}
|
||||
if orEq {
|
||||
v = rval{k: vUndet}
|
||||
}
|
||||
p.consts[name] = v
|
||||
if v.k == vConst && p.w != nil {
|
||||
p.w.define(name, p.w.kind[v.s])
|
||||
if p.w != nil {
|
||||
k := byte(kVal)
|
||||
if v.k == vConst {
|
||||
k = p.w.kind[v.s]
|
||||
}
|
||||
p.w.define(name, k)
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (p *parser) strictConstAssign() error {
|
||||
name := p.cur().text
|
||||
if ok, _ := p.constAssign(); !ok {
|
||||
return fmt.Errorf("unsupported non-literal constant %s", name)
|
||||
ok, err := p.constAssign()
|
||||
if err == nil && !ok {
|
||||
err = fmt.Errorf("unsupported non-literal constant %s", name)
|
||||
}
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
|
||||
// iniConsts are the Puppet::Util::IniConfig and FileType constants require 'puppet/util/inifile' defines.
|
||||
@@ -494,7 +507,6 @@ var (
|
||||
"puppet/network/http_pool": {"Puppet::Network m", "Puppet::Network::HttpPool m"},
|
||||
"puppet/type/file": fileConsts, "puppet/type/file/owner": fileConsts, "puppet/type/file/group": fileConsts, "puppet/type/file/mode": fileConsts,
|
||||
}
|
||||
coreConsts = set("Puppet PuppetX Type ResourceApi")
|
||||
)
|
||||
|
||||
// topStmt allows only statements that cannot reach a type: requires, literal constants, module Puppet and the type definition.
|
||||
@@ -520,7 +532,7 @@ func (p *parser) topStmt(inPuppet bool) func() (bool, error) {
|
||||
return true, fmt.Errorf("Puppet::ResourceApi used without require 'puppet/resource_api'")
|
||||
}
|
||||
return true, p.registerType()
|
||||
case t.kind == tConst && p.assigns() && !coreConsts[t.text]:
|
||||
case t.kind == tConst && p.assigns():
|
||||
return true, p.strictConstAssign()
|
||||
}
|
||||
return true, fmt.Errorf("unsupported top-level %s", p.construct())
|
||||
|
||||
Reference in New Issue
Block a user