Skip to content

Commit 59002b8

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents 917af5d + 0909bcd commit 59002b8

File tree

24 files changed

+361
-92
lines changed

24 files changed

+361
-92
lines changed

src/cmd/compile/internal/ssa/_gen/ARM64.rules

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,16 +1385,18 @@
13851385
(CSNEG [cc] x y (InvertFlags cmp)) => (CSNEG [arm64Invert(cc)] x y cmp)
13861386

13871387
// absorb flag constants into boolean values
1388-
(Equal (FlagConstant [fc])) => (MOVDconst [b2i(fc.eq())])
1389-
(NotEqual (FlagConstant [fc])) => (MOVDconst [b2i(fc.ne())])
1390-
(LessThan (FlagConstant [fc])) => (MOVDconst [b2i(fc.lt())])
1391-
(LessThanU (FlagConstant [fc])) => (MOVDconst [b2i(fc.ult())])
1392-
(LessEqual (FlagConstant [fc])) => (MOVDconst [b2i(fc.le())])
1393-
(LessEqualU (FlagConstant [fc])) => (MOVDconst [b2i(fc.ule())])
1394-
(GreaterThan (FlagConstant [fc])) => (MOVDconst [b2i(fc.gt())])
1395-
(GreaterThanU (FlagConstant [fc])) => (MOVDconst [b2i(fc.ugt())])
1396-
(GreaterEqual (FlagConstant [fc])) => (MOVDconst [b2i(fc.ge())])
1397-
(GreaterEqualU (FlagConstant [fc])) => (MOVDconst [b2i(fc.uge())])
1388+
(Equal (FlagConstant [fc])) => (MOVDconst [b2i(fc.eq())])
1389+
(NotEqual (FlagConstant [fc])) => (MOVDconst [b2i(fc.ne())])
1390+
(LessThan (FlagConstant [fc])) => (MOVDconst [b2i(fc.lt())])
1391+
(LessThanU (FlagConstant [fc])) => (MOVDconst [b2i(fc.ult())])
1392+
(LessEqual (FlagConstant [fc])) => (MOVDconst [b2i(fc.le())])
1393+
(LessEqualU (FlagConstant [fc])) => (MOVDconst [b2i(fc.ule())])
1394+
(GreaterThan (FlagConstant [fc])) => (MOVDconst [b2i(fc.gt())])
1395+
(GreaterThanU (FlagConstant [fc])) => (MOVDconst [b2i(fc.ugt())])
1396+
(GreaterEqual (FlagConstant [fc])) => (MOVDconst [b2i(fc.ge())])
1397+
(GreaterEqualU (FlagConstant [fc])) => (MOVDconst [b2i(fc.uge())])
1398+
(LessThanNoov (FlagConstant [fc])) => (MOVDconst [b2i(fc.ltNoov())])
1399+
(GreaterEqualNoov (FlagConstant [fc])) => (MOVDconst [b2i(fc.geNoov())])
13981400

13991401
// absorb InvertFlags into boolean values
14001402
(Equal (InvertFlags x)) => (Equal x)

src/cmd/compile/internal/ssa/rewriteARM64.go

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/context/context.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,11 +288,17 @@ func withCancel(parent Context) *cancelCtx {
288288
func Cause(c Context) error {
289289
if cc, ok := c.Value(&cancelCtxKey).(*cancelCtx); ok {
290290
cc.mu.Lock()
291-
defer cc.mu.Unlock()
292-
return cc.cause
291+
cause := cc.cause
292+
cc.mu.Unlock()
293+
if cause != nil {
294+
return cause
295+
}
296+
// Either this context is not canceled,
297+
// or it is canceled and the cancellation happened in a
298+
// custom context implementation rather than a *cancelCtx.
293299
}
294-
// There is no cancelCtxKey value, so we know that c is
295-
// not a descendant of some Context created by WithCancelCause.
300+
// There is no cancelCtxKey value with a cause, so we know that c is
301+
// not a descendant of some canceled Context created by WithCancelCause.
296302
// Therefore, there is no specific cause to return.
297303
// If this is not one of the standard Context types,
298304
// it might still have an error even though it won't have a cause.

src/context/x_test.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,45 @@ func TestCause(t *testing.T) {
798798
err: nil,
799799
cause: nil,
800800
},
801+
{
802+
name: "parent of custom context not canceled",
803+
ctx: func() Context {
804+
ctx, _ := WithCancelCause(Background())
805+
ctx, cancel2 := newCustomContext(ctx)
806+
cancel2()
807+
return ctx
808+
},
809+
err: Canceled,
810+
cause: Canceled,
811+
},
812+
{
813+
name: "parent of custom context is canceled before",
814+
ctx: func() Context {
815+
ctx, cancel1 := WithCancelCause(Background())
816+
ctx, cancel2 := newCustomContext(ctx)
817+
cancel1(parentCause)
818+
cancel2()
819+
return ctx
820+
},
821+
err: Canceled,
822+
cause: parentCause,
823+
},
824+
{
825+
name: "parent of custom context is canceled after",
826+
ctx: func() Context {
827+
ctx, cancel1 := WithCancelCause(Background())
828+
ctx, cancel2 := newCustomContext(ctx)
829+
cancel2()
830+
cancel1(parentCause)
831+
return ctx
832+
},
833+
err: Canceled,
834+
// This isn't really right: the child context was canceled before
835+
// the parent context, and shouldn't inherit the parent's cause.
836+
// However, since the child is a custom context, Cause has no way
837+
// to tell which was canceled first and returns the parent's cause.
838+
cause: parentCause,
839+
},
801840
} {
802841
test := test
803842
t.Run(test.name, func(t *testing.T) {
@@ -1089,3 +1128,52 @@ func TestAfterFuncCalledAsynchronously(t *testing.T) {
10891128
t.Fatalf("AfterFunc not called after context is canceled")
10901129
}
10911130
}
1131+
1132+
// customContext is a custom Context implementation.
1133+
type customContext struct {
1134+
parent Context
1135+
1136+
doneOnce sync.Once
1137+
donec chan struct{}
1138+
err error
1139+
}
1140+
1141+
func newCustomContext(parent Context) (Context, CancelFunc) {
1142+
c := &customContext{
1143+
parent: parent,
1144+
donec: make(chan struct{}),
1145+
}
1146+
AfterFunc(parent, func() {
1147+
c.doneOnce.Do(func() {
1148+
c.err = parent.Err()
1149+
close(c.donec)
1150+
})
1151+
})
1152+
return c, func() {
1153+
c.doneOnce.Do(func() {
1154+
c.err = Canceled
1155+
close(c.donec)
1156+
})
1157+
}
1158+
}
1159+
1160+
func (c *customContext) Deadline() (time.Time, bool) {
1161+
return c.parent.Deadline()
1162+
}
1163+
1164+
func (c *customContext) Done() <-chan struct{} {
1165+
return c.donec
1166+
}
1167+
1168+
func (c *customContext) Err() error {
1169+
select {
1170+
case <-c.donec:
1171+
return c.err
1172+
default:
1173+
return nil
1174+
}
1175+
}
1176+
1177+
func (c *customContext) Value(key any) any {
1178+
return c.parent.Value(key)
1179+
}

src/go/ast/import.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ func SortImports(fset *token.FileSet, f *File) {
3333
for j, s := range d.Specs {
3434
if j > i && lineAt(fset, s.Pos()) > 1+lineAt(fset, d.Specs[j-1].End()) {
3535
// j begins a new run. End this one.
36-
specs = append(specs, sortSpecs(fset, f, d.Specs[i:j])...)
36+
specs = append(specs, sortSpecs(fset, f, d, d.Specs[i:j])...)
3737
i = j
3838
}
3939
}
40-
specs = append(specs, sortSpecs(fset, f, d.Specs[i:])...)
40+
specs = append(specs, sortSpecs(fset, f, d, d.Specs[i:])...)
4141
d.Specs = specs
4242

4343
// Deduping can leave a blank line before the rparen; clean that up.
@@ -109,7 +109,7 @@ type cgPos struct {
109109
cg *CommentGroup
110110
}
111111

112-
func sortSpecs(fset *token.FileSet, f *File, specs []Spec) []Spec {
112+
func sortSpecs(fset *token.FileSet, f *File, d *GenDecl, specs []Spec) []Spec {
113113
// Can't short-circuit here even if specs are already sorted,
114114
// since they might yet need deduplication.
115115
// A lone import, however, may be safely ignored.
@@ -207,7 +207,11 @@ func sortSpecs(fset *token.FileSet, f *File, specs []Spec) []Spec {
207207
deduped = append(deduped, s)
208208
} else {
209209
p := s.Pos()
210-
fset.File(p).MergeLine(lineAt(fset, p))
210+
// This function is exited early when len(specs) <= 1,
211+
// so d.Rparen must be populated (d.Rparen.IsValid() == true).
212+
if l := lineAt(fset, p); l != lineAt(fset, d.Rparen) {
213+
fset.File(p).MergeLine(l)
214+
}
211215
}
212216
}
213217
specs = deduped

src/go/ast/import_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,44 @@ import (
7979
}
8080
})
8181
}
82+
83+
func TestIssue69183(t *testing.T) {
84+
const src = `package A
85+
import (
86+
"a"//a
87+
"a")
88+
`
89+
fset := token.NewFileSet()
90+
f, err := parser.ParseFile(fset, "test.go", src, parser.ParseComments|parser.SkipObjectResolution)
91+
if err != nil {
92+
t.Fatal(err)
93+
}
94+
ast.SortImports(fset, f) // should not panic
95+
}
96+
97+
func TestSortImportsSameLastLine(t *testing.T) {
98+
const src = `package A
99+
import (
100+
"a"//a
101+
"a")
102+
func a() {}
103+
`
104+
105+
fset := token.NewFileSet()
106+
f, err := parser.ParseFile(fset, "test.go", src, parser.ParseComments|parser.SkipObjectResolution)
107+
if err != nil {
108+
t.Fatal(err)
109+
}
110+
ast.SortImports(fset, f)
111+
fd := f.Decls[1].(*ast.FuncDecl)
112+
fdPos := fset.Position(fd.Pos())
113+
// After SortImports, the Position of the func, should still be at Column == 1.
114+
// This is related to the issue: https://go.dev/issue/69183, we were merging lines
115+
// incorrectly, which caused the position to be Column = 6, Line = 4.
116+
if fdPos.Column != 1 {
117+
t.Errorf("invalid fdPos.Column = %v; want = 1", fdPos.Column)
118+
}
119+
if fdPos.Line != 5 {
120+
t.Errorf("invalid fdPos.Line = %v; want = 5", fdPos.Line)
121+
}
122+
}

src/go/types/api.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,9 @@ type Info struct {
254254
//
255255
// For an embedded field, Defs returns the field *Var it defines.
256256
//
257+
// In ill-typed code, such as a duplicate declaration of the
258+
// same name, Defs may lack an entry for a declaring identifier.
259+
//
257260
// Invariant: Defs[id] == nil || Defs[id].Pos() == id.Pos()
258261
Defs map[*ast.Ident]Object
259262

src/internal/runtime/maps/runtime_fast32_swiss.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
func runtime_mapaccess1_fast32(typ *abi.SwissMapType, m *Map, key uint32) unsafe.Pointer {
1818
if race.Enabled && m != nil {
1919
callerpc := sys.GetCallerPC()
20-
pc := abi.FuncPCABIInternal(runtime_mapaccess1)
20+
pc := abi.FuncPCABIInternal(runtime_mapaccess1_fast32)
2121
race.ReadPC(unsafe.Pointer(m), callerpc, pc)
2222
}
2323

@@ -86,7 +86,7 @@ func runtime_mapaccess1_fast32(typ *abi.SwissMapType, m *Map, key uint32) unsafe
8686
func runtime_mapaccess2_fast32(typ *abi.SwissMapType, m *Map, key uint32) (unsafe.Pointer, bool) {
8787
if race.Enabled && m != nil {
8888
callerpc := sys.GetCallerPC()
89-
pc := abi.FuncPCABIInternal(runtime_mapaccess1)
89+
pc := abi.FuncPCABIInternal(runtime_mapaccess2_fast32)
9090
race.ReadPC(unsafe.Pointer(m), callerpc, pc)
9191
}
9292

@@ -198,7 +198,7 @@ func runtime_mapassign_fast32(typ *abi.SwissMapType, m *Map, key uint32) unsafe.
198198
}
199199
if race.Enabled {
200200
callerpc := sys.GetCallerPC()
201-
pc := abi.FuncPCABIInternal(runtime_mapassign)
201+
pc := abi.FuncPCABIInternal(runtime_mapassign_fast32)
202202
race.WritePC(unsafe.Pointer(m), callerpc, pc)
203203
}
204204
if m.writing != 0 {
@@ -332,7 +332,7 @@ func runtime_mapassign_fast32ptr(typ *abi.SwissMapType, m *Map, key unsafe.Point
332332
}
333333
if race.Enabled {
334334
callerpc := sys.GetCallerPC()
335-
pc := abi.FuncPCABIInternal(runtime_mapassign)
335+
pc := abi.FuncPCABIInternal(runtime_mapassign_fast32ptr)
336336
race.WritePC(unsafe.Pointer(m), callerpc, pc)
337337
}
338338
if m.writing != 0 {
@@ -458,7 +458,7 @@ outer:
458458
func runtime_mapdelete_fast32(typ *abi.SwissMapType, m *Map, key uint32) {
459459
if race.Enabled {
460460
callerpc := sys.GetCallerPC()
461-
pc := abi.FuncPCABIInternal(runtime_mapassign)
461+
pc := abi.FuncPCABIInternal(runtime_mapdelete_fast32)
462462
race.WritePC(unsafe.Pointer(m), callerpc, pc)
463463
}
464464

src/internal/runtime/maps/runtime_fast64_swiss.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
func runtime_mapaccess1_fast64(typ *abi.SwissMapType, m *Map, key uint64) unsafe.Pointer {
1818
if race.Enabled && m != nil {
1919
callerpc := sys.GetCallerPC()
20-
pc := abi.FuncPCABIInternal(runtime_mapaccess1)
20+
pc := abi.FuncPCABIInternal(runtime_mapaccess1_fast64)
2121
race.ReadPC(unsafe.Pointer(m), callerpc, pc)
2222
}
2323

@@ -86,7 +86,7 @@ func runtime_mapaccess1_fast64(typ *abi.SwissMapType, m *Map, key uint64) unsafe
8686
func runtime_mapaccess2_fast64(typ *abi.SwissMapType, m *Map, key uint64) (unsafe.Pointer, bool) {
8787
if race.Enabled && m != nil {
8888
callerpc := sys.GetCallerPC()
89-
pc := abi.FuncPCABIInternal(runtime_mapaccess1)
89+
pc := abi.FuncPCABIInternal(runtime_mapaccess2_fast64)
9090
race.ReadPC(unsafe.Pointer(m), callerpc, pc)
9191
}
9292

@@ -198,7 +198,7 @@ func runtime_mapassign_fast64(typ *abi.SwissMapType, m *Map, key uint64) unsafe.
198198
}
199199
if race.Enabled {
200200
callerpc := sys.GetCallerPC()
201-
pc := abi.FuncPCABIInternal(runtime_mapassign)
201+
pc := abi.FuncPCABIInternal(runtime_mapassign_fast64)
202202
race.WritePC(unsafe.Pointer(m), callerpc, pc)
203203
}
204204
if m.writing != 0 {
@@ -370,7 +370,7 @@ func runtime_mapassign_fast64ptr(typ *abi.SwissMapType, m *Map, key unsafe.Point
370370
}
371371
if race.Enabled {
372372
callerpc := sys.GetCallerPC()
373-
pc := abi.FuncPCABIInternal(runtime_mapassign)
373+
pc := abi.FuncPCABIInternal(runtime_mapassign_fast64ptr)
374374
race.WritePC(unsafe.Pointer(m), callerpc, pc)
375375
}
376376
if m.writing != 0 {
@@ -497,7 +497,7 @@ outer:
497497
func runtime_mapdelete_fast64(typ *abi.SwissMapType, m *Map, key uint64) {
498498
if race.Enabled {
499499
callerpc := sys.GetCallerPC()
500-
pc := abi.FuncPCABIInternal(runtime_mapassign)
500+
pc := abi.FuncPCABIInternal(runtime_mapdelete_fast64)
501501
race.WritePC(unsafe.Pointer(m), callerpc, pc)
502502
}
503503

0 commit comments

Comments
 (0)