Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the suggestion in the error when an interface is used as a type #281

Merged
merged 1 commit into from
Aug 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1313,24 +1313,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