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

resolve: allow if/for/while statements at toplevel #103

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

alandonovan
Copy link
Contributor

These constructs are still disallowed in the Bazel build language,
but it is easy to check for them as a separate pass over the syntax tree,
so there is no need to complicate the spec with this restriction,
which is a nuisance in other dialects.

Clients that set the -globalreassign flag to suppress the check
need do so no longer.

@alandonovan
Copy link
Contributor Author

@laurentlb

These constructs are still disallowed in the Bazel build language,
but it is easy to check for them as a separate pass over the syntax tree,
so there is no need to complicate the spec with this restriction,
which is a nuisance in other dialects.

Clients that set the -globalreassign flag to suppress the check
need do so no longer.

Change-Id: I39ddb2128e6151a878a884fdb84d7017df2d5d51
Copy link
Collaborator

@jayconrod jayconrod left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code here seems sound, but I'm not sure I understand the premise. if and for are still forbidden at top-level in the canonical Starlark spec. It's part of the language, right, not Bazel's validation? It seems like this should still be behind a dialect flag.

@alandonovan
Copy link
Contributor Author

Correct. I am suggesting that the Starlark spec should say the same thing, and we specify the restrictions in to the Bazel manual. Ping @laurentlb

@alandonovan
Copy link
Contributor Author

alandonovan commented Jan 9, 2019

It seems like this should still be behind a dialect flag.

Bazel already needs to inspect the AST for other reasons, for example, it disallows f(**kwargs) in a BUILD file. So it already has a place to do this check, and it already has (I assume) a place in its manual to explain the notion of syntax restrictions.

@laurentlb
Copy link
Contributor

Can you have it behind a flag for now?

@alandonovan
Copy link
Contributor Author

Can you have it behind a flag for now?

Have what behind a flag, exactly? A Bazel-like tool using Starlark-go already must inspect the syntax tree to reject def and f(**kwargs) in BUILD files, so it is trivial for it to also check for toplevel if/for/while at that point by adding these 8 lines of code:

	for _, stmt := range f.Stmts {
		switch stmt := stmt.(type) {
		case *syntax.IfStmt:
			// error: if-statements are not allowed at top level
		case *syntax.ForStmt:
			// error: for loops are not allowed at top level
		}
	}

No other client besides Bazel seems to need or want this behavior.

@laurentlb
Copy link
Contributor

As it doesn't follow the specification, I think it's preferable to have a flag to allow if/for at top-level. Users should explicitly opt in if they prefer this behavior.

An if statement is permitted only within a function definition. An if statement at top level results in a static error. (https://github.com/bazelbuild/starlark/blob/master/spec.md#if-statements)
In Starlark, a for loop is permitted only within a function definition. A for loop at top level results in a static error.

@alandonovan
Copy link
Contributor Author

As it doesn't follow the specification, I think it's preferable...

Laurent, we own the specification and it is wrong. Let's change it.

@AlekSi
Copy link
Contributor

AlekSi commented Jan 31, 2020

  -globalreassign
    	allow reassignment of globals, and if/for/while statements at top level

Isn't that supported already? Is this PR stale?

@alandonovan
Copy link
Contributor Author

This PR is really a request to change the spec to match reality: top-level if/for are permitted in all dialects except for Bazel, which imposes various unique restrictions on BUILD and .bzl files, this being one of them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants