Array literal can be used in array and slice
This commit is contained in:
parent
45574d3cd5
commit
b839517e9a
@ -54,21 +54,29 @@ func (this *Tree) analyzeLiteralArray (
|
|||||||
entity.Expression,
|
entity.Expression,
|
||||||
error,
|
error,
|
||||||
) {
|
) {
|
||||||
base, ok := this.reduceToBase(into).(*entity.TypeArray)
|
into = this.reduceToBase(into)
|
||||||
if !ok {
|
var elementType entity.Type
|
||||||
|
switch into.(type) {
|
||||||
|
case *entity.TypeArray:
|
||||||
|
into := into.(*entity.TypeArray)
|
||||||
|
if into.Length != len(literal.Elements) {
|
||||||
|
return nil, participle.Errorf (
|
||||||
|
literal.Pos, "expected %v elements",
|
||||||
|
into.Length)
|
||||||
|
}
|
||||||
|
elementType = into.Element
|
||||||
|
|
||||||
|
case *entity.TypeSlice:
|
||||||
|
into := into.(*entity.TypeSlice)
|
||||||
|
elementType = into.Element
|
||||||
|
default:
|
||||||
return nil, participle.Errorf (
|
return nil, participle.Errorf (
|
||||||
literal.Pos, "cannot use array literal as %v",
|
literal.Pos, "cannot use array literal as %v",
|
||||||
into)
|
into)
|
||||||
}
|
}
|
||||||
|
|
||||||
if base.Length != len(literal.Elements) {
|
|
||||||
return nil, participle.Errorf (
|
|
||||||
literal.Pos, "expected %v elements",
|
|
||||||
base.Length)
|
|
||||||
}
|
|
||||||
|
|
||||||
for index, element := range literal.Elements {
|
for index, element := range literal.Elements {
|
||||||
element, err := this.analyzeExpression(base.Element, strict, element)
|
element, err := this.analyzeExpression(elementType, strict, element)
|
||||||
if err != nil { return nil, err }
|
if err != nil { return nil, err }
|
||||||
literal.Elements[index] = element
|
literal.Elements[index] = element
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user