Skip to content

Commit ceb4787

Browse files
committed
refactor(compiler): improve clarity of common type error messages
- Enhanced UnificationFailed with 'Type mismatch' prefix - Clarified reference return errors with advice on using '@' or owned values - Improved NotAType and NotAmongRegisteredTypes with advice for external types - Refined wording for better developer experience
1 parent f9925ae commit ceb4787

1 file changed

Lines changed: 24 additions & 15 deletions

File tree

src/TypeError.hs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ instance Show TypeError where
148148
++ prettyInfoFromXObj xobj
149149
++ ".\n\nI need exactly one body form. For multiple forms, try using `do`."
150150
show (UnificationFailed (Constraint a b aObj bObj ctx _) mappings _) =
151-
"I can’t match the types `" ++ showTy a ++ "` and `" ++ showTy b ++ "`."
151+
"Type mismatch: I can’t match the types `" ++ showTy a ++ "` and `" ++ showTy b ++ "`."
152152
++ extra
153153
++ showObj aObj
154154
++ showObj bObj
@@ -212,16 +212,16 @@ instance Show TypeError where
212212
show (NotAValidType xobj) =
213213
pretty xobj ++ " is not a valid type at " ++ prettyInfoFromXObj xobj
214214
show (FunctionsCantReturnRefTy xobj t) =
215-
"Functions can’t return references. " ++ getName xobj ++ " : " ++ show t
215+
"Functions can’t return references. The function '" ++ getName xobj ++ "' has the type " ++ show t
216216
++ " at "
217217
++ prettyInfoFromXObj xobj
218-
++ "\n\nYou’ll have to copy the return value using `@`."
218+
++ ".\n\nYou’ll have to copy the return value using `@` or return an owned value."
219219
show (LetCantReturnRefTy xobj t) =
220-
"`let` expressions can’t return references. " ++ pretty xobj ++ " : "
220+
"`let` expressions can’t return references. The expression '" ++ pretty xobj ++ "' has the type "
221221
++ show t
222222
++ " at "
223223
++ prettyInfoFromXObj xobj
224-
++ "\n\nYou’ll have to copy the return value using `@`."
224+
++ ".\n\nYou’ll have to copy the return value using `@` or return an owned value."
225225
show (GettingReferenceToUnownedValue xobj) =
226226
"You’re referencing a given-away value `" ++ pretty xobj ++ "` at "
227227
++ prettyInfoFromXObj xobj --"' (expression " ++ freshVar i ++ ") at " ++
@@ -236,17 +236,22 @@ instance Show TypeError where
236236
"You’re using a value `" ++ pretty xobj
237237
++ "` that was captured by a function at "
238238
++ prettyInfoFromXObj xobj
239-
++ "."
239+
++ ".\n\nCaptured values can't be moved. You'll have to borrow it using `&` or copy it using `@`."
240240
show (ArraysCannotContainRefs xobj) =
241241
"Arrays can’t contain references: `" ++ pretty xobj ++ "` at "
242242
++ prettyInfoFromXObj xobj
243243
++ ".\n\nYou’ll have to make a copy using `@`."
244-
show (MainCanOnlyReturnUnitOrInt _ t) =
245-
"The main function can only return an `Int` or a unit type (`()`), but it got `"
244+
show (MainCanOnlyReturnUnitOrInt xobj t) =
245+
"The main function can only return an `Int` or a unit type `()`, but it got `"
246246
++ show t
247-
++ "`."
248-
show (MainCannotHaveArguments _ c) =
249-
"The main function may not receive arguments, but it got " ++ show c ++ "."
247+
++ "` at "
248+
++ prettyInfoFromXObj xobj
249+
++ "."
250+
show (MainCannotHaveArguments xobj c) =
251+
"The main function may not receive arguments, but it got " ++ show c
252+
++ " at "
253+
++ prettyInfoFromXObj xobj
254+
++ "."
250255
show (CannotConcretize xobj) =
251256
"I’m unable to concretize the expression '" ++ pretty xobj ++ "' at "
252257
++ prettyInfoFromXObj xobj
@@ -259,7 +264,7 @@ instance Show TypeError where
259264
show (NotAType xobj) =
260265
"I don’t understand the type '" ++ pretty xobj ++ "' at "
261266
++ prettyInfoFromXObj xobj
262-
++ "\n\nIs it defined?"
267+
++ ".\n\nIs it defined? If it's an external type, make sure it's registered using `register-type`."
263268
show (CannotSet xobj) =
264269
"I can’t `set!` the expression `" ++ pretty xobj ++ "` at "
265270
++ prettyInfoFromXObj xobj
@@ -303,7 +308,7 @@ instance Show TypeError where
303308
show (NotAmongRegisteredTypes t xobj) =
304309
"I can’t find a definition for the type `" ++ show t ++ "` at "
305310
++ prettyInfoFromXObj xobj
306-
++ ".\n\nWas it registered?"
311+
++ ".\n\nIs it defined? If it's an external type, make sure it's registered using `register-type`."
307312
show (UnevenMembers xobjs) =
308313
"The number of members and types is uneven: `"
309314
++ joinWithComma (map pretty xobjs)
@@ -320,11 +325,15 @@ instance Show TypeError where
320325
++ prettyInfoFromXObj (head xobjs)
321326
++ ". \n\n Binding names must be symbols."
322327
show (DuplicateBinding xobj) =
323-
"I encountered a duplicate binding `" ++ pretty xobj ++ "` inside the `let` at " ++ prettyInfoFromXObj xobj ++ "."
328+
"I encountered a duplicate binding `" ++ pretty xobj ++ "` inside the `let` at "
329+
++ prettyInfoFromXObj xobj
330+
++ ".\n\nEach name in a `let` must be unique."
324331
show (DefinitionsMustBeAtToplevel xobj) =
325332
"I encountered a definition that was not at top level: `" ++ pretty xobj ++ "`"
326333
show (UsingDeadReference xobj dependsOn) =
327-
"The reference '" ++ pretty xobj ++ "' (depending on the variable '" ++ dependsOn ++ "') isn't alive at " ++ prettyInfoFromXObj xobj ++ "."
334+
"The reference '" ++ pretty xobj ++ "' is no longer valid because the value it depends on (`" ++ dependsOn ++ "`) has been moved or deleted at "
335+
++ prettyInfoFromXObj xobj
336+
++ "."
328337
show (UninhabitedConstructor ty xobj got wanted) =
329338
"Can't use a struct or sumtype constructor without arguments as a member type at " ++ prettyInfoFromXObj xobj ++ ". The type constructor " ++ show ty ++ " expects " ++ show wanted ++ " arguments but got " ++ show got
330339
show (InconsistentKinds varName xobjs) =

0 commit comments

Comments
 (0)