case *CallExpr:
if name, _ := x.Fun.(*Name); name != nil {
if len(x.ArgList) == 1 && !x.HasDots && (force || isTypeElem(x.ArgList[0])) {
- // x = name "(" x.ArgList[0] ")"
- return name, x.ArgList[0]
+ // The parser doesn't keep unnecessary parentheses.
+ // Set the flag below to keep them, for testing
+ // (see go.dev/issues/69206).
+ const keep_parens = false
+ if keep_parens {
+ // x = name (x.ArgList[0])
+ px := new(ParenExpr)
+ px.pos = x.pos // position of "(" in call
+ px.X = x.ArgList[0]
+ return name, px
+ } else {
+ // x = name x.ArgList[0]
+ return name, Unparen(x.ArgList[0])
+ }
}
}
}
}
p.print(blank)
}
- p.printNode(Unparen(f.Type)) // no need for (extra) parentheses around parameter types
+ p.printNode(f.Type)
}
// A type parameter list [P T] where the name P and the type expression T syntactically
// combine to another valid (value) expression requires a trailing comma, as in [P *T,]
// binary expressions
return combinesWithName(x.X) && !isTypeElem(x.Y)
case *ParenExpr:
- // name(x) combines but we are making sure at
- // the call site that x is never parenthesized.
- panic("unexpected parenthesized expression")
+ // Note that the parser strips parentheses in these cases
+ // (see extractName, parser.typeOrNil) unless keep_parens
+ // is set, so we should never reach here.
+ // Do the right thing (rather than panic) for testing and
+ // in case we change parser behavior.
+ // See also go.dev/issues/69206.
+ return !isTypeElem(x.X)
}
return false
}
dup("package p; type _ chan<- <-chan int"),
dup("package p; type _ chan<- chan<- int"),
+ // go.dev/issues/69206
+ dup("package p; type _[P C] int"),
+ {"package p; type _[P (C),] int", "package p; type _[P C] int"},
+ {"package p; type _[P ((C)),] int", "package p; type _[P C] int"},
+ {"package p; type _[P, Q ((C))] int", "package p; type _[P, Q C] int"},
+
// TODO(gri) expand
}