Skip to content

Commit 40b7fe4

Browse files
Fixed requested changes
1 parent 948c73a commit 40b7fe4

File tree

4 files changed

+44
-38
lines changed

4 files changed

+44
-38
lines changed

compiler/functions.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ func (fc *funcContext) translateFunctionBody(typ *ast.FuncType, recv *ast.Ident,
237237
}
238238

239239
bodyOutput := string(fc.CatchOutput(1, func() {
240-
if fc.HasBlocking() {
240+
if fc.IsBlocking() {
241241
fc.pkgCtx.Scopes[body] = fc.pkgCtx.Scopes[typ]
242242
fc.handleEscapingVars(body)
243243
}
@@ -283,14 +283,14 @@ func (fc *funcContext) translateFunctionBody(typ *ast.FuncType, recv *ast.Ident,
283283
if fc.HasDefer {
284284
fc.localVars = append(fc.localVars, "$deferred")
285285
suffix = " }" + suffix
286-
if fc.HasBlocking() {
286+
if fc.IsBlocking() {
287287
suffix = " }" + suffix
288288
}
289289
}
290290

291291
localVarDefs := "" // Function-local var declaration at the top.
292292

293-
if fc.HasBlocking() {
293+
if fc.IsBlocking() {
294294
localVars := append([]string{}, fc.localVars...)
295295
// There are several special variables involved in handling blocking functions:
296296
// $r is sometimes used as a temporary variable to store blocking call result.
@@ -314,7 +314,7 @@ func (fc *funcContext) translateFunctionBody(typ *ast.FuncType, recv *ast.Ident,
314314
if fc.HasDefer {
315315
prefix = prefix + " var $err = null; try {"
316316
deferSuffix := " } catch(err) { $err = err;"
317-
if fc.HasBlocking() {
317+
if fc.IsBlocking() {
318318
deferSuffix += " $s = -1;"
319319
}
320320
if fc.resultNames == nil && fc.sig.HasResults() {
@@ -324,7 +324,7 @@ func (fc *funcContext) translateFunctionBody(typ *ast.FuncType, recv *ast.Ident,
324324
if fc.resultNames != nil {
325325
deferSuffix += fmt.Sprintf(" if (!$curGoroutine.asleep) { return %s; }", fc.translateResults(fc.resultNames))
326326
}
327-
if fc.HasBlocking() {
327+
if fc.IsBlocking() {
328328
deferSuffix += " if($curGoroutine.asleep) {"
329329
}
330330
suffix = deferSuffix + suffix

compiler/internal/analysis/info.go

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func (info *Info) newFuncInfoInstances(fd *ast.FuncDecl) []*FuncInfo {
123123
// IsBlocking returns true if the function may contain blocking calls or operations.
124124
func (info *Info) IsBlocking(inst typeparams.Instance) bool {
125125
if funInfo := info.FuncInfo(inst); funInfo != nil {
126-
return funInfo.HasBlocking()
126+
return funInfo.IsBlocking()
127127
}
128128
panic(fmt.Errorf(`info did not have function declaration instance for %q`, inst))
129129
}
@@ -193,7 +193,7 @@ func AnalyzePkg(files []*ast.File, fileSet *token.FileSet, typesInfo *types.Info
193193
for _, caller := range info.allInfos {
194194
// Check calls to named functions and function-typed variables.
195195
caller.localInstCallees.Iterate(func(callee typeparams.Instance, callSites []astPath) {
196-
if info.FuncInfo(callee).HasBlocking() {
196+
if info.FuncInfo(callee).IsBlocking() {
197197
for _, callSite := range callSites {
198198
caller.markBlocking(callSite)
199199
}
@@ -204,7 +204,7 @@ func AnalyzePkg(files []*ast.File, fileSet *token.FileSet, typesInfo *types.Info
204204

205205
// Check direct calls to function literals.
206206
for callee, callSites := range caller.literalFuncCallees {
207-
if info.FuncLitInfo(callee).HasBlocking() {
207+
if info.FuncLitInfo(callee).IsBlocking() {
208208
for _, callSite := range callSites {
209209
caller.markBlocking(callSite)
210210
}
@@ -267,11 +267,11 @@ type FuncInfo struct {
267267
visitorStack astPath
268268
}
269269

270-
// HasBlocking indicates if this function may block goroutine execution.
270+
// IsBlocking indicates if this function may block goroutine execution.
271271
//
272272
// For example, a channel operation in a function or a call to another
273273
// possibly blocking function may block the function.
274-
func (fi *FuncInfo) HasBlocking() bool {
274+
func (fi *FuncInfo) IsBlocking() bool {
275275
return fi == nil || len(fi.Blocking) != 0
276276
}
277277

@@ -397,19 +397,22 @@ func (fi *FuncInfo) visitCallExpr(n *ast.CallExpr) ast.Visitor {
397397
switch f := astutil.RemoveParens(n.Fun).(type) {
398398
case *ast.Ident:
399399
fi.callToNamedFunc(fi.instanceForIdent(f))
400+
return fi
400401
case *ast.SelectorExpr:
401402
if sel := fi.pkgInfo.Selections[f]; sel != nil {
402403
if typesutil.IsJsObject(sel.Recv()) {
403404
// js.Object methods are known to be non-blocking, but we still must
404405
// check its arguments.
405-
} else {
406-
// selection is a method call like `foo.Bar()`, where `foo` might
407-
// be generic and needs to be substituted with the type argument.
408-
fi.callToNamedFunc(fi.instanceFoSelection(sel))
406+
return fi
409407
}
410-
} else {
411-
fi.callToNamedFunc(fi.instanceForIdent(f.Sel))
408+
// selection is a method call like `foo.Bar()`, where `foo` might
409+
// be generic and needs to be substituted with the type argument.
410+
fi.callToNamedFunc(fi.instanceForSelection(sel))
411+
return fi
412412
}
413+
414+
fi.callToNamedFunc(fi.instanceForIdent(f.Sel))
415+
return fi
413416
case *ast.FuncLit:
414417
// Collect info about the function literal itself.
415418
ast.Walk(fi, n.Fun)
@@ -427,40 +430,43 @@ func (fi *FuncInfo) visitCallExpr(n *ast.CallExpr) ast.Visitor {
427430
// This is a type conversion to an instance of a generic type,
428431
// not a call. Type assertion itself is not blocking, but we will
429432
// visit the input expression.
430-
} else if astutil.IsTypeExpr(f.Index, fi.pkgInfo.Info) {
433+
return fi
434+
}
435+
if astutil.IsTypeExpr(f.Index, fi.pkgInfo.Info) {
431436
// This is a call of an instantiation of a generic function,
432437
// e.g. `foo[int]` in `func foo[T any]() { ... }; func main() { foo[int]() }`
433438
fi.callToNamedFunc(fi.instanceForIdent(f.X.(*ast.Ident)))
434-
} else {
435-
// The called function is gotten with an index or key from a map, array, or slice.
436-
// e.g. `m := map[string]func(){}; m["key"]()`, `s := []func(); s[0]()`.
437-
// Since we can't predict if the returned function will be blocking
438-
// or not, we have to be conservative and assume that function might be blocking.
439-
fi.markBlocking(fi.visitorStack)
439+
return fi
440440
}
441+
// The called function is gotten with an index or key from a map, array, or slice.
442+
// e.g. `m := map[string]func(){}; m["key"]()`, `s := []func(); s[0]()`.
443+
// Since we can't predict if the returned function will be blocking
444+
// or not, we have to be conservative and assume that function might be blocking.
445+
fi.markBlocking(fi.visitorStack)
446+
return fi
441447
case *ast.IndexListExpr:
442448
// Collect info about the instantiated type or function.
443449
if astutil.IsTypeExpr(f, fi.pkgInfo.Info) {
444450
// This is a type conversion to an instance of a generic type,
445451
// not a call. Type assertion itself is not blocking, but we will
446452
// visit the input expression.
447-
} else {
448-
// This is a call of an instantiation of a generic function,
449-
// e.g. `foo[int, bool]` in `func foo[T1, T2 any]() { ... }; func main() { foo[int, bool]() }`
450-
fi.callToNamedFunc(fi.instanceForIdent(f.X.(*ast.Ident)))
453+
return fi
451454
}
455+
// This is a call of an instantiation of a generic function,
456+
// e.g. `foo[int, bool]` in `func foo[T1, T2 any]() { ... }; func main() { foo[int, bool]() }`
457+
fi.callToNamedFunc(fi.instanceForIdent(f.X.(*ast.Ident)))
458+
return fi
452459
default:
453460
if astutil.IsTypeExpr(f, fi.pkgInfo.Info) {
454461
// This is a type conversion, not a call. Type assertion itself is not
455462
// blocking, but we will visit the input expression.
456-
} else {
457-
// The function is returned by a non-trivial expression. We have to be
458-
// conservative and assume that function might be blocking.
459-
fi.markBlocking(fi.visitorStack)
463+
return fi
460464
}
465+
// The function is returned by a non-trivial expression. We have to be
466+
// conservative and assume that function might be blocking.
467+
fi.markBlocking(fi.visitorStack)
468+
return fi
461469
}
462-
463-
return fi
464470
}
465471

466472
func (fi *FuncInfo) instanceForIdent(fnId *ast.Ident) typeparams.Instance {
@@ -471,7 +477,7 @@ func (fi *FuncInfo) instanceForIdent(fnId *ast.Ident) typeparams.Instance {
471477
}
472478
}
473479

474-
func (fi *FuncInfo) instanceFoSelection(sel *types.Selection) typeparams.Instance {
480+
func (fi *FuncInfo) instanceForSelection(sel *types.Selection) typeparams.Instance {
475481
if _, ok := sel.Obj().Type().(*types.Signature); ok {
476482
// Substitute the selection to ensure that the receiver has the correct
477483
// type arguments propagated down from the caller.

compiler/internal/analysis/info_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,7 +1211,7 @@ func (bt *blockingTest) isFuncLitBlocking(lineNo int) bool {
12111211
if fnLit == nil {
12121212
bt.f.T.Fatalf(`FuncLit found on line %d not found in the AST.`, lineNo)
12131213
}
1214-
return bt.pkgInfo.FuncLitInfo(fnLit).HasBlocking()
1214+
return bt.pkgInfo.FuncLitInfo(fnLit).IsBlocking()
12151215
}
12161216

12171217
func (bt *blockingTest) assertBlockingInst(instanceStr string) {
@@ -1230,7 +1230,7 @@ func (bt *blockingTest) isFuncInstBlocking(instanceStr string) bool {
12301230
instances := bt.pkgInfo.funcInstInfos.Keys()
12311231
for _, inst := range instances {
12321232
if inst.TypeString() == instanceStr {
1233-
return bt.pkgInfo.FuncInfo(inst).HasBlocking()
1233+
return bt.pkgInfo.FuncInfo(inst).IsBlocking()
12341234
}
12351235
}
12361236
bt.f.T.Logf(`Function instances found in package info:`)

compiler/internal/typeparams/instance.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ func (iset *InstanceSet) ByObj() map[types.Object][]Instance {
147147
return result
148148
}
149149

150-
// ForObj returns instances for a given object type belong to. Order is not specified.
151-
// This returns the same values as `ByObj()[obj]`.
150+
// ForObj returns the instances that belong to the given object type.
151+
// Order is not specified. This returns the same values as `ByObj()[obj]`.
152152
func (iset *InstanceSet) ForObj(obj types.Object) []Instance {
153153
result := []Instance{}
154154
for _, inst := range iset.values {

0 commit comments

Comments
 (0)