diff options
author | Simon Peyton Jones <simonpj@microsoft.com> | 2021-07-27 18:07:11 +0100 |
---|---|---|
committer | Marge Bot <ben+marge-bot@smart-cactus.org> | 2022-02-04 20:36:20 -0500 |
commit | 6af8e71ed7e749ba94e7a7eaf8b2229341bf35da (patch) | |
tree | aabf6c233d2067ca9f62b5c5ff4ec83576e58bd9 /compiler/GHC/Tc | |
parent | bf495f7206741c81135c04ce6bb943c4a6729e80 (diff) | |
download | haskell-6af8e71ed7e749ba94e7a7eaf8b2229341bf35da.tar.gz |
Improve errors for non-existent labels
This patch fixes #17469, by improving matters when you use
non-existent field names in a record construction:
data T = MkT { x :: Int }
f v = MkT { y = 3 }
The check is now made in the renamer, in GHC.Rename.Env.lookupRecFieldOcc.
That in turn led to a spurious error in T9975a, which is fixed by
making GHC.Rename.Names.extendGlobalRdrEnvRn fail fast if it finds
duplicate bindings. See Note [Fail fast on duplicate definitions]
in that module for more details.
This patch was originated and worked on by Alex D (@nineonine)
Diffstat (limited to 'compiler/GHC/Tc')
-rw-r--r-- | compiler/GHC/Tc/Gen/Expr.hs | 3 | ||||
-rw-r--r-- | compiler/GHC/Tc/Gen/Pat.hs | 9 | ||||
-rw-r--r-- | compiler/GHC/Tc/Utils/Monad.hs | 2 |
3 files changed, 3 insertions, 11 deletions
diff --git a/compiler/GHC/Tc/Gen/Expr.hs b/compiler/GHC/Tc/Gen/Expr.hs index 230acdc3f5..46775235df 100644 --- a/compiler/GHC/Tc/Gen/Expr.hs +++ b/compiler/GHC/Tc/Gen/Expr.hs @@ -52,7 +52,6 @@ import GHC.Tc.Utils.Env import GHC.Tc.Gen.Arrow import GHC.Tc.Gen.Match import GHC.Tc.Gen.HsType -import GHC.Tc.Gen.Pat import GHC.Tc.Utils.TcMType import GHC.Tc.Types.Origin import GHC.Tc.Utils.TcType as TcType @@ -1399,7 +1398,7 @@ tcRecordField con_like flds_w_tys (L loc (FieldOcc sel_name lbl)) rhs -- (so the desugarer knows the type of local binder to make) ; return (Just (L loc (FieldOcc field_id lbl), rhs')) } | otherwise - = do { addErrTc (badFieldCon con_like field_lbl) + = do { addErrTc (badFieldConErr (getName con_like) field_lbl) ; return Nothing } where field_lbl = occNameFS $ rdrNameOcc (unLoc lbl) diff --git a/compiler/GHC/Tc/Gen/Pat.hs b/compiler/GHC/Tc/Gen/Pat.hs index 6034d05720..132f58b7b4 100644 --- a/compiler/GHC/Tc/Gen/Pat.hs +++ b/compiler/GHC/Tc/Gen/Pat.hs @@ -21,7 +21,6 @@ module GHC.Tc.Gen.Pat , tcCheckPat, tcCheckPat_O, tcInferPat , tcPats , addDataConStupidTheta - , badFieldCon , polyPatSig ) where @@ -1282,7 +1281,7 @@ tcConArgs con_like arg_tys tenv penv con_args thing_inside = case con_args of -- f (R { foo = (a,b) }) = a+b -- If foo isn't one of R's fields, we don't want to crash when -- typechecking the "a+b". - [] -> failWith (badFieldCon con_like lbl) + [] -> failWith (badFieldConErr (getName con_like) lbl) -- The normal case, when the field comes from the right constructor (pat_ty : extras) -> do @@ -1489,12 +1488,6 @@ checkGADT conlike ex_tvs arg_tys = \case has_existentials :: Bool has_existentials = any (`elemVarSet` tyCoVarsOfTypes arg_tys) ex_tvs -badFieldCon :: ConLike -> FieldLabelString -> TcRnMessage -badFieldCon con field - = TcRnUnknownMessage $ mkPlainError noHints $ - hsep [text "Constructor" <+> quotes (ppr con), - text "does not have field", quotes (ppr field)] - polyPatSig :: TcType -> SDoc polyPatSig sig_ty = hang (text "Illegal polymorphic type signature in pattern:") diff --git a/compiler/GHC/Tc/Utils/Monad.hs b/compiler/GHC/Tc/Utils/Monad.hs index ea3b50fa3c..ca2915e8fa 100644 --- a/compiler/GHC/Tc/Utils/Monad.hs +++ b/compiler/GHC/Tc/Utils/Monad.hs @@ -1142,7 +1142,7 @@ reportDiagnostic msg ----------------------- checkNoErrs :: TcM r -> TcM r -- (checkNoErrs m) succeeds iff m succeeds and generates no errors --- If m fails then (checkNoErrsTc m) fails. +-- If m fails then (checkNoErrs m) fails. -- If m succeeds, it checks whether m generated any errors messages -- (it might have recovered internally) -- If so, it fails too. |