Skip to content

compiler: report errors for misused pragmas - #5936

Merged
deadprogram merged 1 commit into
tinygo-org:devfrom
davecheney:davecheney-harden-pragmas
Oct 20, 2026
Merged

deadprogram merged 1 commit into
tinygo-org:devfrom
davecheney:davecheney-harden-pragmas

Conversation

@davecheney

Copy link
Copy Markdown
Contributor

Report malformed pragma parameters and invalid placements instead of silently ignoring them. Reject invalid global extern, align, and section parameters without panicking.

Use source AST bodies for function placement checks, including generic origins and unbuilt dependencies. Preserve package-level unsafe gating and report each source pragma error once.

Add regression coverage for declarations, definitions, methods, generic instantiations, and exact diagnostics.

Verified with TINYGOROOT="$PWD" go test -tags=llvm22 ./cgo ./compiler ./transform -count=1 and the root pragma, noruntime, and noruntime-use error tests.

Before this change, most pragmas silently did nothing when they had extra parameters, were on the wrong kind of function, or were in a package that does not import unsafe. A //go:align without a parameter caused a panic. These mistakes are easy to make and hard to find, because the build passes and only the generated code is different.

Flag pragmas such as //go:noinline and //go:noheap now reject parameters with one shared check. Each pragma then checks its own placement rules. The rules for //go:noescape are the same as in gc. The checks for the other pragmas follow the conditions that TinyGo already used to decide whether to apply them.
@davecheney

Copy link
Copy Markdown
Contributor Author

Thanks for merging #5894 , that change unblocked this which adds stricter validation for pragmas rather than silently ignoring them.

@dgryski dgryski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@deadprogram

Copy link
Copy Markdown
Member

Thanks for the addition @davecheney and to @dgryski for review. Now merging.

@deadprogram
deadprogram merged commit f4eaef5 into tinygo-org:dev Oct 20, 2026
31 checks passed
@davecheney
davecheney deleted the davecheney-harden-pragmas branch October 20, 2026 21:10
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.

3 participants