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

Improve type errors for record and tuple lookups #2213

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 1 addition & 3 deletions intTests/test_type_errors/err020.log.good
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
Loading file "err020.saw"
err020.saw:3:20-3:33: Record lookup on non-record argument.
Field name: nonexistent

err020.saw:3:20-3:33: Record lookup on non-record value of type Int
FAILED
4 changes: 1 addition & 3 deletions intTests/test_type_errors/err021.log.good
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
Loading file "err021.saw"
err021.saw:8:18-8:31: Selecting a missing field.
Field name: nonexistent

err021.saw:8:18-8:31: Record type has no field named nonexistent
FAILED
4 changes: 1 addition & 3 deletions intTests/test_type_errors/err022.log.good
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
Loading file "err022.saw"
err022.saw:3:20-3:23: Tuple lookup on non-tuple argument.
Given index 3

err022.saw:3:20-3:23: Tuple lookup on non-tuple value of type Int
FAILED
4 changes: 1 addition & 3 deletions intTests/test_type_errors/err023.log.good
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
Loading file "err023.saw"
err023.saw:5:18-5:21: Tuple index out of bounds.
Given index 3 is too large for tuple size of 2

err023.saw:5:18-5:21: Tuple index 3 out of bounds; limit is 2
FAILED
3 changes: 3 additions & 0 deletions intTests/test_type_errors/err046.log.good
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Loading file "err046.saw"
err046.saw:3:13-3:24: Cannot infer a record type for field someField; please use a type annotation
FAILED
3 changes: 3 additions & 0 deletions intTests/test_type_errors/err046.saw
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Trigger the message about not supporting record inference.

let foo x = x.someField;
3 changes: 3 additions & 0 deletions intTests/test_type_errors/err047.log.good
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Loading file "err047.saw"
err047.saw:3:13-3:16: Cannot infer tuple arity for lookup of element 3; please use a type annotation
FAILED
3 changes: 3 additions & 0 deletions intTests/test_type_errors/err047.saw
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Trigger the message about not supporting tuple arity inference.

let foo x = x.3;
61 changes: 33 additions & 28 deletions src/SAWScript/MGU.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1045,40 +1045,45 @@ inferExpr (ln, expr) = case expr of
do (e1,t) <- inferExpr (ln, e)
t1 <- applyCurrentSubst =<< resolveCurrentTypedefs t
elTy <- case t1 of
TyRecord typos fs
| Just ty <- M.lookup n fs -> return ty
| otherwise ->
do recordError pos $ unlines
[ "Selecting a missing field."
, "Field name: " ++ Text.unpack n
]
getErrorTyVar typos
_ -> do recordError pos $ unlines
[ "Record lookup on non-record argument."
, "Field name: " ++ Text.unpack n
]
getErrorTyVar pos
TyRecord typos fs
| Just ty <- M.lookup n fs -> do
return ty
| otherwise -> do
recordError pos $
"Record type has no field named " ++ Text.unpack n
getErrorTyVar typos
TyUnifyVar _ _ -> do
recordError pos $
"Cannot infer a record type for field " ++
Text.unpack n ++ "; please use a type annotation"
getErrorTyVar pos
_ -> do
recordError pos $
"Record lookup on non-record value of type " ++ pShow t1
getErrorTyVar pos
return (Lookup pos e1 n, elTy)

TLookup pos e i ->
do (e1,t) <- inferExpr (ln,e)
t1 <- applyCurrentSubst =<< resolveCurrentTypedefs t
elTy <- case t1 of
TyCon typos (TupleCon n) tys
| i < n -> return (tys !! fromIntegral i)
| otherwise ->
do recordError pos $ unlines
[ "Tuple index out of bounds."
, "Given index " ++ show i ++
" is too large for tuple size of " ++
show n
]
getErrorTyVar typos
_ -> do recordError pos $ unlines
[ "Tuple lookup on non-tuple argument."
, "Given index " ++ show i
]
getErrorTyVar pos
TyCon typos (TupleCon n) tys
| i < n ->
return (tys !! fromIntegral i)
| otherwise -> do
recordError pos $
"Tuple index " ++ show i ++ " out of bounds; limit is " ++
show n
getErrorTyVar typos
TyUnifyVar _ _ -> do
recordError pos $
"Cannot infer tuple arity for lookup of element " ++
show i ++ "; please use a type annotation"
getErrorTyVar pos
_ -> do
recordError pos $
"Tuple lookup on non-tuple value of type " ++ pShow t1
getErrorTyVar pos
return (TLookup pos e1 i, elTy)

Var x ->
Expand Down
Loading