Skip to content

Commit

Permalink
lang: Hack! Do not merge! Show the callee of type errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix Frank committed Sep 10, 2024
1 parent 03a9b87 commit ebb8255
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lang/ast/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8415,7 +8415,7 @@ func (obj *ExprCall) Infer() (*types.Type, []*interfaces.UnificationInvariant, e
var typExpr *types.Type // out

// Look at what kind of function we are calling...
callee := trueCallee(obj.expr)
callee := TrueCallee(obj.expr)
exprFunc, isFn := callee.(*ExprFunc)

argGen := func(x int) (string, error) {
Expand Down
10 changes: 5 additions & 5 deletions lang/ast/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,16 +327,16 @@ func getScope(node interfaces.Expr) (*interfaces.Scope, error) {
}
}

// trueCallee is a helper function because ExprTopLevel and ExprSingleton are
// TrueCallee is a helper function because ExprTopLevel and ExprSingleton are
// sometimes added around builtins. This makes it difficult for the type checker
// to check if a particular builtin is the callee or not. This function removes
// the ExprTopLevel and ExprSingleton wrappers, if they exist.
func trueCallee(apparentCallee interfaces.Expr) interfaces.Expr {
func TrueCallee(apparentCallee interfaces.Expr) interfaces.Expr {
switch x := apparentCallee.(type) {
case *ExprTopLevel:
return trueCallee(x.Definition)
return TrueCallee(x.Definition)
case *ExprSingleton:
return trueCallee(x.Definition)
return TrueCallee(x.Definition)
default:
return apparentCallee
}
Expand Down Expand Up @@ -383,7 +383,7 @@ func lambdaScopeFeedback(scope *interfaces.Scope, logf func(format string, v ...
names := []string{}
for name, val := range scope.Variables {
// XXX: Is this a valid way to filter?
if _, ok := trueCallee(val).(*ExprFunc); !ok {
if _, ok := TrueCallee(val).(*ExprFunc); !ok {
continue
}
names = append(names, name)
Expand Down
3 changes: 2 additions & 1 deletion lang/gapi/gapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,8 @@ func (obj *GAPI) Cli(info *gapi.Info) (*gapi.Deploy, error) {
if args.OnlyUnify {
logf("type unification failed after %s", formatted)
}
return nil, errwrap.Wrapf(unifyErr, "could not unify types")
callee := ast.TrueCallee(unifyErr.(*interfaces.UnificationInvariant).Expr)
return nil, errwrap.Wrapf(unifyErr, "could not unify types [[ in expression: %s ]]", callee)
}

if args.OnlyUnify {
Expand Down
7 changes: 7 additions & 0 deletions lang/interfaces/unification.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ type UnificationInvariant struct { // formerly the SamInvariant

// Actual is one of the two types to unify.
Actual *types.Type

// An error string to pass along with this
Err string
}

func (obj *UnificationInvariant) Error() string {
return obj.Err
}

// GenericCheck is the generic implementation of the Check Expr interface call.
Expand Down
5 changes: 3 additions & 2 deletions lang/unification/fastsolver/fastsolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ type FastInvariantSolver struct {
func (obj *FastInvariantSolver) Init(init *unification.Init) error {
obj.Strategy = init.Strategy
obj.UnifiedState = init.UnifiedState
obj.Debug = init.Debug
obj.Debug = true //init.Debug
obj.Logf = init.Logf

optimizations, exists := init.Strategy[unification.StrategyOptimizationsKey]
Expand Down Expand Up @@ -135,7 +135,8 @@ func (obj *FastInvariantSolver) Solve(ctx context.Context, data *unification.Dat
// Storing the Expr with this invariant is so that we
// can generate this more helpful error message here.
// TODO: Improve this error message!
return nil, errwrap.Wrapf(err, "unify error with: %s", x.Expr)
x.Err = errwrap.Wrapf(err, "unify error with: %s", x.Expr).Error()
return nil, x
}
if obj.Debug {
e1, e2 := unificationUtil.Extract(x.Expect), unificationUtil.Extract(x.Actual)
Expand Down
1 change: 1 addition & 0 deletions lang/unification/unification.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type Unifier struct {
// any proper literature or examples describing a well-known implementation of
// this process. Improvements and polite recommendations are welcome.
func (obj *Unifier) Unify(ctx context.Context) error {
obj.Debug = true
if obj.AST == nil {
return fmt.Errorf("the AST is nil")
}
Expand Down

0 comments on commit ebb8255

Please sign in to comment.