Skip to content

Commit

Permalink
internal/wire: fix crash when giving wire.Struct a bad first argument (
Browse files Browse the repository at this point in the history
  • Loading branch information
zombiezen authored and shantuo committed Nov 11, 2019
1 parent 0d3b2eb commit a5347c8
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/wire/parse.go
Expand Up @@ -805,7 +805,7 @@ func processStructProvider(fset *token.FileSet, info *types.Info, call *ast.Call
st, ok := structPtr.Elem().Underlying().(*types.Struct)
if !ok {
return nil, notePosition(fset.Position(call.Pos()),
fmt.Errorf(firstArgReqFormat, types.TypeString(st, nil)))
fmt.Errorf(firstArgReqFormat, types.TypeString(structPtr, nil)))
}

stExpr := call.Args[0].(*ast.CallExpr)
Expand Down
27 changes: 27 additions & 0 deletions internal/wire/testdata/StructNotAStruct/foo/foo.go
@@ -0,0 +1,27 @@
// Copyright 2019 The Wire Authors
//
// 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
//
// https://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 main

import (
"fmt"
)

func main() {
fmt.Println(inject(A{"Hello"}))
}

type A struct {
B string
}
26 changes: 26 additions & 0 deletions internal/wire/testdata/StructNotAStruct/foo/wire.go
@@ -0,0 +1,26 @@
// Copyright 2019 The Wire Authors
//
// 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
//
// https://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.

//+build wireinject

package main

import (
"github.com/google/wire"
)

func inject(a A) string {
wire.Build(wire.Struct(new(*A), "*"))
return ""
}
1 change: 1 addition & 0 deletions internal/wire/testdata/StructNotAStruct/pkg
@@ -0,0 +1 @@
example.com/foo
1 change: 1 addition & 0 deletions internal/wire/testdata/StructNotAStruct/want/wire_errs.txt
@@ -0,0 +1 @@
example.com/foo/wire.go:x:y: first argument to Struct must be a pointer to a named struct; found **example.com/foo.A

0 comments on commit a5347c8

Please sign in to comment.