Skip to content

Commit

Permalink
fix the suggestion in the error when an interface is used as a type
Browse files Browse the repository at this point in the history
recursively rewrite instead of just at root of type
  • Loading branch information
turbolent committed Aug 4, 2020
1 parent de4c8a7 commit af04532
Show file tree
Hide file tree
Showing 5 changed files with 301 additions and 127 deletions.
6 changes: 4 additions & 2 deletions runtime/sema/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2081,10 +2081,12 @@ func (checker *Checker) checkTypeAnnotation(typeAnnotation *TypeAnnotation, pos
)
}

if typeAnnotation.Type.ContainsFirstLevelInterfaceType() {
rewrittenType, rewritten := typeAnnotation.Type.RewriteWithRestrictedTypes()
if rewritten {
checker.report(
&InvalidInterfaceTypeError{
Type: typeAnnotation.Type,
ActualType: typeAnnotation.Type,
ExpectedType: rewrittenType,
Range: ast.Range{
StartPos: pos.StartPosition(),
EndPos: pos.EndPosition(),
Expand Down
16 changes: 6 additions & 10 deletions runtime/sema/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -1326,24 +1326,20 @@ func (*InvalidResourceAnnotationError) isSemanticError() {}
// InvalidInterfaceTypeError

type InvalidInterfaceTypeError struct {
Type Type
ActualType Type
ExpectedType Type
ast.Range
}

func (e *InvalidInterfaceTypeError) Error() string {
return "invalid interface type"
return "invalid use of interface as type"
}

func (e *InvalidInterfaceTypeError) SecondaryError() string {
var restrictedAny Type = &AnyStructType{}
if e.Type.IsResourceType() {
restrictedAny = &AnyResourceType{}
}

return fmt.Sprintf(
"got `%[1]s`; consider using `%[2]s{%[1]s}`",
e.Type.QualifiedString(),
restrictedAny,
"got `%s`; consider using `%s`",
e.ActualType.QualifiedString(),
e.ExpectedType.QualifiedString(),
)
}

Expand Down
Loading

0 comments on commit af04532

Please sign in to comment.