Skip to content

Commit

Permalink
compiler/checker: type check shown files with a different type checker
Browse files Browse the repository at this point in the history
This change type checks the shown files with a new type checker, that
creates a clean "type checking environment" that is then discarded.

This fixes the bug #660 that was caused by an invalid value of
'tc.ancestors' (it was inherited from the type checking of the "outer"
file) that made 'tc.currentFunction' (and consequently 'isUpVar') return
wrong results.

Using a new type checker (that is discarded after checking the shown
file) is now possible because the type infos have been moved inside the
'compilation', so they are not lost even if the type checker is deleted.

This change may also avoids some other similar problems.

Fix #660.
  • Loading branch information
zapateo committed Oct 16, 2020
1 parent 88269ac commit 887cf9f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
26 changes: 12 additions & 14 deletions compiler/checker_statements.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,20 +138,18 @@ nodesLoop:
case *ast.Text:

case *ast.ShowPartial:

// Check the shown tree in a separate scope, that cannot access to
// variables declared in the scope of the partial file or even add
// declarations to it.

path := tc.path
tc.path = node.Tree.Path
scopes := tc.scopes
tc.scopes = nil

node.Tree.Nodes = tc.checkNodesInNewScope(node.Tree.Nodes)

tc.path = path
tc.scopes = scopes
// Type check the shown tree with a separate type checker.
// The scope of the shown file is independent from the scope of the
// file that shows it (except for global declarations).
// Also, the use of a different type checker ensures that the
// fields of 'tc' are not altered nor inherited.
tc2 := newTypechecker(
tc.compilation,
node.Tree.Path,
tc.opts,
tc.globalScope,
)
tc2.checkNodes(node.Tree.Nodes)

case *ast.Block:
node.Nodes = tc.checkNodesInNewScope(node.Nodes)
Expand Down
6 changes: 6 additions & 0 deletions templates/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1769,6 +1769,12 @@ var templateMultiPageCases = map[string]struct {
"shown.html": `{% var v int %}`,
},
},
"https://github.com/open2b/scriggo/issues/660": {
sources: map[string]string{
"index.html": `{% macro M() %}{% show "shown.html" %}{% end macro %}`,
"shown.html": `{% var v int %}{% _ = v %}`,
},
},
}

var structWithUnexportedFields = &struct {
Expand Down

0 comments on commit 887cf9f

Please sign in to comment.