-
Notifications
You must be signed in to change notification settings - Fork 49
cmd/compile: validate noescape function bodies #2220
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
Closed
cpunion
wants to merge
2
commits into
xgo-dev:main
from
cpunion:codex/xfail-noescape-pragma-20260730
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| /* | ||
| * Copyright (c) 2026 The XGo Authors (xgo.dev). All rights reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package packages | ||
|
|
||
| import ( | ||
| "go/ast" | ||
| "go/token" | ||
| "go/types" | ||
| "strings" | ||
| ) | ||
|
|
||
| const noescapeBodyDiagnostic = "can only use //go:noescape with external func implementations" | ||
|
|
||
| // validateCompilerDirectives applies cmd/compile's //go:noescape body check, | ||
| // which is outside go/types. The go command may report this check while loading | ||
| // export data, but it can stop before reaching it when an earlier compiler error | ||
| // is present, so the source frontend must validate it independently. | ||
| func validateCompilerDirectives(fset *token.FileSet, files []*ast.File) []types.Error { | ||
| if fset == nil { | ||
| return nil | ||
| } | ||
| var errs []types.Error | ||
| for _, file := range files { | ||
| if file == nil { | ||
| continue | ||
| } | ||
| previousEnd := file.Name.End() | ||
| for _, decl := range file.Decls { | ||
| fn, ok := decl.(*ast.FuncDecl) | ||
| directiveEnd := decl.Pos() | ||
| var declarationToken token.Pos | ||
| if ok { | ||
| declarationToken = fn.Pos() | ||
| directiveEnd = compilerFunctionPos(fn) | ||
| } | ||
| hasNoescape := hasCompilerDirectiveBetween( | ||
| fset, file.Comments, previousEnd, directiveEnd, declarationToken, "go:noescape", | ||
| ) | ||
| previousEnd = decl.End() | ||
| if !ok || fn.Body == nil || !hasNoescape { | ||
| continue | ||
| } | ||
| errs = append(errs, types.Error{ | ||
| Fset: fset, | ||
| Pos: directiveEnd, | ||
| Msg: noescapeBodyDiagnostic, | ||
| }) | ||
| } | ||
| } | ||
| return errs | ||
| } | ||
|
|
||
| func compilerFunctionPos(fn *ast.FuncDecl) token.Pos { | ||
| if fn.Recv != nil && fn.Recv.Opening.IsValid() { | ||
| return fn.Recv.Opening | ||
| } | ||
| if fn.Name != nil { | ||
| return fn.Name.Pos() | ||
| } | ||
| return fn.Pos() | ||
| } | ||
|
|
||
| // hasCompilerDirectiveBetween reports whether a standalone directive occurs | ||
| // between two declarations. cmd/compile keeps directives pending across blank | ||
| // lines and ordinary comments, then consumes them at the next declaration. A | ||
| // function directive may also occur between the func keyword and its name (or | ||
| // receiver). | ||
| func hasCompilerDirectiveBetween( | ||
| fset *token.FileSet, groups []*ast.CommentGroup, start, end, declarationToken token.Pos, directive string, | ||
| ) bool { | ||
| previous := start | ||
| for _, group := range groups { | ||
| if group == nil || group.End() <= start || group.Pos() >= end { | ||
| continue | ||
| } | ||
| for _, comment := range group.List { | ||
| if comment == nil || comment.End() <= start || comment.Pos() >= end { | ||
| continue | ||
| } | ||
| if declarationToken > previous && declarationToken < comment.Pos() { | ||
| previous = declarationToken | ||
| } | ||
| standalone := physicalLine(fset, previous) != physicalLine(fset, comment.Pos()) | ||
| previous = comment.End() | ||
| if !standalone || !strings.HasPrefix(comment.Text, "//") { | ||
| continue | ||
| } | ||
| text := strings.TrimPrefix(comment.Text, "//") | ||
| if text == directive { | ||
| return true | ||
| } | ||
| if strings.HasPrefix(text, directive) && len(text) > len(directive) { | ||
| next := text[len(directive)] | ||
| if next == ' ' { | ||
| return true | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| func physicalLine(fset *token.FileSet, pos token.Pos) int { | ||
| return fset.PositionFor(pos, false).Line | ||
| } | ||
|
|
||
| func packageHasCompilerDiagnostic(errs []Error, want types.Error) bool { | ||
| position := want.Fset.Position(want.Pos) | ||
| for _, err := range errs { | ||
| if err.Msg == want.Msg && sameDiagnosticLine(err.Pos, position) { | ||
| return true | ||
| } | ||
| for _, line := range strings.Split(err.Msg, "\n") { | ||
| pos, msg, ok := strings.Cut(line, ": ") | ||
| if ok && msg == want.Msg && sameDiagnosticLine(pos, position) { | ||
| return true | ||
| } | ||
| } | ||
| } | ||
| return false | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.