[Git][ghc/ghc][wip/int-index/ie-refactor] Refactor the definition of IEWrappedName (#27267)
Vladislav Zavialov pushed to branch wip/int-index/ie-refactor at Glasgow Haskell Compiler / GHC Commits: 08166c5b by Vladislav Zavialov at 2026-11-29T23:27:19+01:00 Refactor the definition of IEWrappedName (#27267) The definition of IEWrappedName is now simplified to data IEWrappedName p = IEName (XIEName p) (NamespaceSpecifier p) (LIdP p) | XIEWrappedName !(XXIEWrappedName p) from the old definition data IEWrappedName p = IEName (XIEName p) (LIdP p) | IEDefault (XIEDefault p) (LIdP p) | IEPattern (XIEPattern p) (LIdP p) | IEType (XIEType p) (LIdP p) | IEData (XIEData p) (LIdP p) | XIEWrappedName !(XXIEWrappedName p) This is done with two changes: 1. Move the IEPattern and IEDefault constructors to IE, reflecting the fact that `pattern P` and `default C` are never legal in subordinate export/import lists. 2. Merge IEName, IEType, and IEData into one constructor by adding a NamespaceSpecifier field to IEName. The resulting representation more closely reflects the source syntax and allows us to remove panics in GHC.Rename.Names.lookupChildren. The -ddump-minimal-imports output now uses the `data` namespace specifier instead of the deprecated `pattern`. - - - - - 17 changed files: - + changelog.d/ie-refactor-27267 - compiler/GHC/Hs/ImpExp.hs - compiler/GHC/HsToCore/Docs.hs - compiler/GHC/Iface/Ext/Ast.hs - compiler/GHC/Parser.y - compiler/GHC/Parser/PostProcess.hs - compiler/GHC/Parser/PostProcess/Haddock.hs - compiler/GHC/Rename/Names.hs - compiler/GHC/Tc/Errors/Ppr.hs - compiler/GHC/Tc/Gen/Export.hs - compiler/Language/Haskell/Syntax/Extension.hs - compiler/Language/Haskell/Syntax/ImpExp.hs - testsuite/tests/haddock/should_compile_flag_haddock/T17544_kw.stderr - testsuite/tests/parser/should_compile/DumpRenamedAst.stderr - testsuite/tests/parser/should_compile/T14189.stderr - utils/check-exact/ExactPrint.hs - utils/check-exact/Main.hs Changes: ===================================== changelog.d/ie-refactor-27267 ===================================== @@ -0,0 +1,15 @@ +section: ghc-lib +synopsis: Refactor the definition of ``IEWrappedName`` +issues: #27267 +mrs: !17080 + +description: { +Move the constructors ``IEDefault`` and ``IEPattern`` from ``IEWrappedName`` to +``IE``, reflecting the fact that ``pattern P`` and ``default C`` are never +legal in subordinate export/import lists. Then simplify ``IEWrappedName`` by +merging its remaining constructors ``IEName``, ``IEData``, and ``IEType`` into +one constructor with a ``NamespaceSpecifier`` field. + +The ``-ddump-minimal-imports`` output now uses the ``data`` namespace specifier +instead of the deprecated ``pattern``. +} ===================================== compiler/GHC/Hs/ImpExp.hs ===================================== @@ -233,10 +233,6 @@ instance (OutputableBndrId p -} type instance XIEName (GhcPass _) = NoExtField -type instance XIEDefault (GhcPass _) = EpToken "default" -type instance XIEPattern (GhcPass _) = EpToken "pattern" -type instance XIEType (GhcPass _) = EpToken "type" -type instance XIEData (GhcPass _) = EpToken "data" type instance XXIEWrappedName (GhcPass _) = DataConCantHappen type instance XNoNamespaceSpecifier (GhcPass _) = NoExtField @@ -283,6 +279,19 @@ type instance XIEThingWith GhcTc = IEThingWithAnns type IEThingWithAnns = (EpToken "(", EpToken "..", EpToken ",", EpToken ")") +-- The additional field of type 'Maybe (WarningTxt pass)' holds information +-- about export deprecation annotations. +type instance XIEDefault GhcPs = (Maybe (LWarningTxt GhcPs), EpToken "default") +type instance XIEDefault GhcRn = Maybe (LWarningTxt GhcRn) +type instance XIEDefault GhcTc = NoExtField + +-- The additional field of type 'Maybe (WarningTxt pass)' holds information +-- about export deprecation annotations and is thus set to Nothing when `IE` +-- is used in an import list (since export deprecation can only be used in exports) +type instance XIEPattern GhcPs = (Maybe (LWarningTxt GhcPs), EpToken "pattern") +type instance XIEPattern GhcRn = Maybe (LWarningTxt GhcRn) +type instance XIEPattern GhcTc = NoExtField + -- The additional field of type 'Maybe (WarningTxt pass)' holds information -- about export deprecation annotations and is thus set to Nothing when `IE` -- is used in an import list (since export deprecation can only be used in exports) @@ -309,26 +318,46 @@ type instance XXIE (GhcPass _) = DataConCantHappen type instance Anno (LocatedA (IE (GhcPass p))) = SrcSpanAnnA ieLIEWrappedName :: IE (GhcPass p) -> LIEWrappedName (GhcPass p) -ieLIEWrappedName (IEVar _ n _) = n -ieLIEWrappedName (IEThingAbs _ n _) = n -ieLIEWrappedName (IEThingWith _ n _ _ _) = n -ieLIEWrappedName (IEThingAll _ _ n _) = n -ieLIEWrappedName _ = panic "ieLIEWrappedName failed pattern match!" +ieLIEWrappedName ie = + case ieLIEWrappedName_maybe ie of + Nothing -> panic "ieLIEWrappedName failed pattern match!" + Just name -> name + +ieLIEWrappedName_maybe :: IE (GhcPass p) -> Maybe (LIEWrappedName (GhcPass p)) +ieLIEWrappedName_maybe (IEVar _ n _) = Just n +ieLIEWrappedName_maybe (IEThingAbs _ n _) = Just n +ieLIEWrappedName_maybe (IEThingWith _ n _ _ _) = Just n +ieLIEWrappedName_maybe (IEThingAll _ _ n _) = Just n +ieLIEWrappedName_maybe _ = Nothing ieName :: IE (GhcPass p) -> IdP (GhcPass p) -ieName = lieWrappedName . ieLIEWrappedName +ieName (IEPattern _ (L _ n) _) = n +ieName (IEDefault _ (L _ n)) = n +ieName ie = lieWrappedName (ieLIEWrappedName ie) ieNames :: IE (GhcPass p) -> [IdP (GhcPass p)] ieNames (IEVar _ (L _ n) _) = [ieWrappedName n] ieNames (IEThingAbs _ (L _ n) _) = [ieWrappedName n] ieNames (IEThingAll _ _ (L _ n) _) = [ieWrappedName n] ieNames (IEThingWith _ (L _ n) _ ns _) = ieWrappedName n : map (ieWrappedName . unLoc) ns +ieNames (IEDefault _ (L _ n)) = [n] +ieNames (IEPattern _ (L _ n) _) = [n] ieNames (IEWholeNamespace x _) = iewn_names x ieNames (IEModuleContents {}) = [] ieNames (IEGroup {}) = [] ieNames (IEDoc {}) = [] ieNames (IEDocNamed {}) = [] +iePatternNameSpec, ieDataNameSpec :: IE (GhcPass p) -> Bool +iePatternNameSpec ie = + case ie of + IEPattern{} -> True + _ -> False +ieDataNameSpec ie = + case ieLIEWrappedName_maybe ie of + Just (L _ (IEName _ DataNamespaceSpecifier{} _)) -> True + _ -> False + ieDeprecation :: forall p. IsPass p => IE (GhcPass p) -> Maybe (WarningTxt (GhcPass p)) ieDeprecation = fmap unLoc . ie_deprecation (ghcPass @p) where @@ -337,24 +366,26 @@ ieDeprecation = fmap unLoc . ie_deprecation (ghcPass @p) ie_deprecation GhcPs (IEThingAbs xie _ _) = xie ie_deprecation GhcPs (IEThingAll xie _ _ _) = ieta_warning xie ie_deprecation GhcPs (IEThingWith (xie, _) _ _ _ _) = xie + ie_deprecation GhcPs (IEDefault (xie, _) _) = xie + ie_deprecation GhcPs (IEPattern (xie, _) _ _) = xie ie_deprecation GhcPs (IEModuleContents (xie, _) _) = xie ie_deprecation GhcRn (IEVar xie _ _) = xie ie_deprecation GhcRn (IEThingAbs xie _ _) = xie ie_deprecation GhcRn (IEThingAll xie _ _ _) = ieta_warning xie ie_deprecation GhcRn (IEThingWith (xie, _) _ _ _ _) = xie + ie_deprecation GhcRn (IEDefault xie _) = xie + ie_deprecation GhcRn (IEPattern xie _ _) = xie ie_deprecation GhcRn (IEModuleContents xie _) = xie ie_deprecation _ _ = Nothing ieWrappedLName :: IEWrappedName (GhcPass p) -> LIdP (GhcPass p) -ieWrappedLName (IEDefault _ (L l n)) = L l n -ieWrappedLName (IEName _ (L l n)) = L l n -ieWrappedLName (IEPattern _ (L l n)) = L l n -ieWrappedLName (IEType _ (L l n)) = L l n -ieWrappedLName (IEData _ (L l n)) = L l n +ieWrappedLName (IEName _ _ n) = n ieWrappedName :: IEWrappedName (GhcPass p) -> IdP (GhcPass p) ieWrappedName = unLoc . ieWrappedLName +ieWrappedNamespaceSpecifier :: IEWrappedName (GhcPass p) -> NamespaceSpecifier (GhcPass p) +ieWrappedNamespaceSpecifier (IEName _ ns_spec _) = ns_spec lieWrappedName :: LIEWrappedName (GhcPass p) -> IdP (GhcPass p) lieWrappedName (L _ n) = ieWrappedName n @@ -362,16 +393,6 @@ lieWrappedName (L _ n) = ieWrappedName n ieLWrappedName :: LIEWrappedName (GhcPass p) -> LIdP (GhcPass p) ieLWrappedName (L _ n) = ieWrappedLName n -replaceWrappedName :: IEWrappedName GhcPs -> IdP GhcRn -> IEWrappedName GhcRn -replaceWrappedName (IEDefault r (L l _)) n = IEDefault r (L l n) -replaceWrappedName (IEName x (L l _)) n = IEName x (L l n) -replaceWrappedName (IEPattern r (L l _)) n = IEPattern r (L l n) -replaceWrappedName (IEType r (L l _)) n = IEType r (L l n) -replaceWrappedName (IEData r (L l _)) n = IEData r (L l n) - -replaceLWrappedName :: LIEWrappedName GhcPs -> IdP GhcRn -> LIEWrappedName GhcRn -replaceLWrappedName (L l n) n' = L l (replaceWrappedName n n') - exportDocstring :: LHsDoc (GhcPass p) -> SDoc exportDocstring doc = braces (text "docstring: " <> ppr doc) @@ -404,6 +425,13 @@ instance OutputableBndrId p => Outputable (IE (GhcPass p)) where IEWildcard pos -> let (bs, as) = splitAt pos (map (ppr . unLoc) withs) in bs ++ [text ".."] ++ as + ppr ie@(IEPattern _ (L _ n) doc) = + sep $ catMaybes [ ppr <$> ieDeprecation ie + , Just $ text "pattern" <+> pprPrefixOcc n + , exportDocstring <$> doc + ] + ppr ie@(IEDefault _ (L _ n)) + = sep $ catMaybes [ppr <$> ieDeprecation ie, Just $ text "default" <+> pprPrefixOcc n] ppr ie@(IEModuleContents _ mod') = sep $ catMaybes [ppr <$> ieDeprecation ie, Just $ text "module" <+> ppr mod'] ppr (IEWholeNamespace _ ns_spec) = ppr ns_spec <+> text ".." @@ -420,11 +448,7 @@ instance OutputableBndrId p => OutputableBndr (IEWrappedName (GhcPass p)) where pprInfixOcc w = pprInfixOcc (ieWrappedName w) instance OutputableBndrId p => Outputable (IEWrappedName (GhcPass p)) where - ppr (IEDefault _ (L _ n)) = text "default" <+> pprPrefixOcc n - ppr (IEName _ (L _ n)) = pprPrefixOcc n - ppr (IEPattern _ (L _ n)) = text "pattern" <+> pprPrefixOcc n - ppr (IEType _ (L _ n)) = text "type" <+> pprPrefixOcc n - ppr (IEData _ (L _ n)) = text "data" <+> pprPrefixOcc n + ppr (IEName _ ns_spec (L _ n)) = ppr ns_spec <+> pprPrefixOcc n pprImpExp :: (HasOccName name, OutputableBndr name) => name -> SDoc pprImpExp name = type_pref <+> pprPrefixOcc name ===================================== compiler/GHC/HsToCore/Docs.hs ===================================== @@ -126,6 +126,8 @@ mkExportsDocs = foldMap f ieExportDoc (IEThingAbs _ _ doc) = doc ieExportDoc (IEThingAll _ _ _ doc) = doc ieExportDoc (IEThingWith _ _ _ _ doc) = doc + ieExportDoc (IEPattern _ _ doc) = doc + ieExportDoc (IEDefault _ _) = Nothing ieExportDoc (IEModuleContents _ _) = Nothing ieExportDoc (IEWholeNamespace _ _) = Nothing ieExportDoc (IEGroup _ _ _) = Nothing ===================================== compiler/GHC/Iface/Ext/Ast.hs ===================================== @@ -2321,6 +2321,12 @@ instance ToHie (IEContext (LocatedA (IE GhcRn))) where [ toHie $ IEC c n , toHie $ map (IEC c) ns ] + IEDefault _ (L l p) -> + [ toHie $ C (IEThing c) (L l p) + ] + IEPattern _ (L l p) _ -> + [ toHie $ C (IEThing c) (L l p) + ] IEModuleContents _ n -> [ toHie $ IEC c n ] @@ -2331,19 +2337,7 @@ instance ToHie (IEContext (LocatedA (IE GhcRn))) where instance ToHie (IEContext (LocatedA (IEWrappedName GhcRn))) where toHie (IEC c (L span iewn)) = concatM $ makeNodeA iewn span : case iewn of - IEDefault _ (L l p) -> - [ toHie $ C (IEThing c) (L l p) - ] - IEName _ (L l n) -> - [ toHie $ C (IEThing c) (L l n) - ] - IEPattern _ (L l p) -> - [ toHie $ C (IEThing c) (L l p) - ] - IEType _ (L l n) -> - [ toHie $ C (IEThing c) (L l n) - ] - IEData _ (L l n) -> + IEName _ _ (L l n) -> [ toHie $ C (IEThing c) (L l n) ] ===================================== compiler/GHC/Parser.y ===================================== @@ -1041,16 +1041,14 @@ export :: { LIE GhcPs } : maybe_warning_pragma qcname_ext export_subspec {% do { let { span = (maybe comb2 comb3 $1) $2 $> } ; impExp <- mkModuleExp $1 (fst $ unLoc $3) $2 (snd $ unLoc $3) ; return $ reLoc $ sL span $ impExp } } - | maybe_warning_pragma 'module' hsmodid {% do { let { span = (maybe comb2 comb3 $1) $2 $> - ; anchor = (maybe glR (\loc -> spanAsAnchor . comb2 loc) $1) $2 } + | maybe_warning_pragma 'module' hsmodid {% do { let { span = (maybe comb2 comb3 $1) $2 $> } ; locImpExp <- return (sL span (IEModuleContents ($1, (epTok $2)) $3)) ; return $ reLoc $ locImpExp } } | maybe_warning_pragma 'pattern' qcon {% do { warnPatternNamespaceSpecifier (getLoc $2) ; let span = (maybe comb2 comb3 $1) $2 $> - ; return $ reLoc $ sL span $ IEVar $1 (sLLa $2 $> (IEPattern (epTok $2) $3)) Nothing } } + ; return $ reLoc $ sL span $ IEPattern ($1, epTok $2) $3 Nothing } } | maybe_warning_pragma 'default' qtycon {% do { let { span = (maybe comb2 comb3 $1) $2 $> } - ; locImpExp <- return (sL span (IEThingAbs $1 (sLLa $2 $> (IEDefault (epTok $2) $3)) Nothing)) - ; return $ reLoc $ locImpExp } } + ; return $ reLoc $ sL span (IEDefault ($1, epTok $2) $3) } } | maybe_warning_pragma 'type' '..' {% do { let { span = (maybe comb2 comb3 $1) $2 $> } ; locImpExp <- mkWholeTypeWcImpExp span $1 (epTok $2) (epTok $3) ; return $ reLoc locImpExp } } @@ -1237,7 +1235,7 @@ import :: { OrdList (LIE GhcPs) } : qcname_ext export_subspec {% fmap (unitOL . reLoc . (sLL $1 $>)) $ mkModuleImp Nothing (fst $ unLoc $2) $1 (snd $ unLoc $2) } | 'module' hsmodid {% fmap (unitOL . reLoc) $ return (sLL $1 $> (IEModuleContents (Nothing, (epTok $1)) $2)) } | 'pattern' qcon {% do { warnPatternNamespaceSpecifier (getLoc $1) - ; return $ unitOL $ reLoc $ sLL $1 $> $ IEVar Nothing (sLLa $1 $> (IEPattern (epTok $1) $2)) Nothing } } + ; return $ unitOL $ reLoc $ sLL $1 $> (IEPattern (Nothing, epTok $1) $2 Nothing) } } | 'type' '..' {% fmap (unitOL . reLoc) $ mkWholeTypeWcImpExp (comb2 $1 $>) Nothing (epTok $1) (epTok $2) } | 'data' '..' {% fmap (unitOL . reLoc) $ mkWholeDataWcImpExp (comb2 $1 $>) Nothing (epTok $1) (epTok $2) } | '..' {% do { addError $ mkPlainErrorMsgEnvelope (gl $1) PsErrPlainWildcardImport ===================================== compiler/GHC/Parser/PostProcess.hs ===================================== @@ -3367,10 +3367,7 @@ mkModuleImpExp ctx warning (top, tcp) (L l specname) subs = do ieNameVal ImpExpQcWildcard{} = panic "ieNameVal got wildcard" ieNameFromSpec :: ImpExpQcSpec -> IEWrappedName GhcPs - ieNameFromSpec (ImpExpQcName m_kw name) = case m_kw of - Nothing -> IEName noExtField name - Just (ExplicitTypeNamespace tok) -> IEType tok name - Just (ExplicitDataNamespace tok) -> IEData tok name + ieNameFromSpec (ImpExpQcName m_kw name) = IEName noExtField (namespaceSpecifierFromKw m_kw) name ieNameFromSpec ImpExpQcWildcard{} = panic "ieNameFromSpec got wildcard" namespaceSpecifierFromKw :: Maybe ExplicitNamespaceKeyword -> NamespaceSpecifier GhcPs ===================================== compiler/GHC/Parser/PostProcess/Haddock.hs ===================================== @@ -319,6 +319,7 @@ instance HasHaddock (LocatedA (IE GhcPs)) where IEThingAbs ext nm _ -> IEThingAbs ext nm mb_ldoc IEThingAll ext ns nm _ -> IEThingAll ext ns nm mb_ldoc IEThingWith ext nm wild subs _ -> IEThingWith ext nm wild subs mb_ldoc + IEPattern ext nm _ -> IEPattern ext nm mb_ldoc x -> x pure $ L l_export ie' ===================================== compiler/GHC/Rename/Names.hs ===================================== @@ -25,7 +25,9 @@ module GHC.Rename.Names ( renamePkgQual, renameRawPkgQual, classifyGREs, ImportDeclUsage, - rnNamespaceSpecifier + rnNamespaceSpecifier, + replaceWrappedName, + replaceLWrappedName, ) where import GHC.Prelude hiding ( head, init, last, tail ) @@ -1222,6 +1224,13 @@ rnNamespaceSpecifier (NoNamespaceSpecifier _) = NoNamespaceSpecifier noExtFiel rnNamespaceSpecifier (TypeNamespaceSpecifier x) = TypeNamespaceSpecifier x rnNamespaceSpecifier (DataNamespaceSpecifier x) = DataNamespaceSpecifier x +replaceWrappedName :: IEWrappedName GhcPs -> Name -> IEWrappedName GhcRn +replaceWrappedName (IEName x ns_spec (L l _)) n = IEName x (rnNamespaceSpecifier ns_spec) (L l n) + +replaceLWrappedName :: LIEWrappedName GhcPs -> Name -> LIEWrappedName GhcRn +replaceLWrappedName (L l n) n' = L l (replaceWrappedName n n') + + filterImports :: HasDebugCallStack => HscEnv @@ -1347,18 +1356,12 @@ filterImports hsc_env iface decl_spec (Just (want_hiding, import_items)) -> IELookupM ([(IE GhcRn, [GlobalRdrElt])], [IELookupWarning]) lookup_ie ie = handle_bad_import $ case ie of - IEVar _ (L l n) _ -> do - -- See Note [Importing DuplicateRecordFields] - xs <- lookup_names ie (ieWrappedName n) - let gres = map imp_item $ NE.toList xs - export_depr_warns - | want_hiding == Exactly - = mapMaybe mk_depr_export_warning gres - | otherwise = [] - return ( [ (IEVar Nothing (L l (replaceWrappedName n name)) noDocstring, [gre]) - | gre <- gres - , let name = greName gre ] - , export_depr_warns ) + IEVar _ (L l n) _ -> + lookup_ie_var (ieWrappedName n) $ \name -> + IEVar Nothing (L l (replaceWrappedName n name)) noDocstring + IEPattern _ (L l n) _ -> + lookup_ie_var n $ \name -> + IEPattern Nothing (L l name) noDocstring IEThingAll x ns_spec (L l tc) _ -> do ImpOccItem { imp_item = gre @@ -1468,6 +1471,19 @@ filterImports hsc_env iface decl_spec (Just (want_hiding, import_items)) -- all of those constitute errors. where + lookup_ie_var n mk_ie = do + -- See Note [Importing DuplicateRecordFields] + xs <- lookup_names ie n + let gres = map imp_item $ NE.toList xs + export_depr_warns + | want_hiding == Exactly + = mapMaybe mk_depr_export_warning gres + | otherwise = [] + return ( [ (mk_ie name, [gre]) + | gre <- gres + , let name = greName gre ] + , export_depr_warns ) + mkIEThingAbs tc l gre = (IEThingAbs Nothing (L l (replaceWrappedName tc n)) noDocstring, [gre]) where n = greName gre @@ -1740,9 +1756,9 @@ lookupChildren all_kids rdr_items = (fails, successes) do_one :: LIEWrappedName GhcPs -> MaybeErr LookupChildError (NonEmpty (LocatedA GlobalRdrElt)) do_one item@(L l r) = - case r of - IEName{} - -- IEName (unadorned name) places no restriction on the namespace of + case ieWrappedNamespaceSpecifier r of + NoNamespaceSpecifier{} + -- An unadorned name places no restriction on the namespace of -- the imported entity, so we look in both `val_gres` and `typ_gres`. -- In case of conflict (punning), the value namespace takes priority. -- See Note [Prioritise the value namespace in subordinate import lists] @@ -1750,24 +1766,21 @@ lookupChildren all_kids rdr_items = (fails, successes) | (gre:gres) <- typ_gres -> Succeeded $ fmap (L l) (gre:|gres) | otherwise -> Failed $ LookupChildNotFound item - IEType{} - -- IEType ('type' namespace specifier) restricts the lookup to the + TypeNamespaceSpecifier{} + -- The 'type' namespace specifier restricts the lookup to the -- type namespace, i.e. to `typ_gres`. In case of failure, we check -- `val_gres` to produce a more helpful error message. | (gre:gres) <- typ_gres -> Succeeded $ fmap (L l) (gre:|gres) | (gre:_) <- val_gres -> Failed $ LookupChildNonType item gre | otherwise -> Failed $ LookupChildNotFound item - IEData{} - -- IEData ('data' namespace specifier) restricts the lookup to the + DataNamespaceSpecifier{} + -- The 'data' namespace specifier restricts the lookup to the -- data namespace, i.e. to `val_gres`. In case of failure, we check -- `typ_gres` to produce a more helpful error message. | (gre:gres) <- val_gres -> Succeeded $ fmap (L l) (gre:|gres) | (gre:_) <- typ_gres -> Failed $ LookupChildNonData item gre | otherwise -> Failed $ LookupChildNotFound item - - IEPattern{} -> panic "lookupChildren: IEPattern" -- Never happens (invalid syntax) - IEDefault{} -> panic "lookupChildren: IEDefault" -- Never happens (invalid syntax) where fs = (occNameFS . rdrNameOcc . ieWrappedName) r gres = fromMaybe [] (lookupFsEnv kid_env fs) @@ -2058,6 +2071,7 @@ findImportUsage imports used_gres add_wc_all = case wc of NoIEWildcard -> id IEWildcard _ -> add_unused_all pn + add_unused (IEPattern _ n _) acc = add_unused_name (unLoc n) True acc add_unused (IEWholeNamespace x ns_spec) acc = add_unused_wildcard ns_spec (iewn_names x) acc add_unused _ acc = acc @@ -2418,16 +2432,20 @@ printMinimalImports hsc_src imports_w_usage to_ie_post_rn_var :: LocatedA (IdP GhcRn) -> LIEWrappedName GhcRn -to_ie_post_rn_var (L l n) - | isDataOcc $ occName n = L l (IEPattern noAnn (L (l2l l) n)) - | otherwise = L l (IEName noExtField (L (l2l l) n)) +to_ie_post_rn_var (L l n) = L l (IEName noExtField ns_spec (L (l2l l) n)) + where + ns_spec + | isDataOcc $ occName n = DataNamespaceSpecifier noAnn + | otherwise = NoNamespaceSpecifier noExtField to_ie_post_rn :: LocatedA (IdP GhcRn) -> LIEWrappedName GhcRn -to_ie_post_rn (L l n) - | isTcOcc occ && isSymOcc occ = L l (IEType noAnn (L (l2l l) n)) - | otherwise = L l (IEName noExtField (L (l2l l) n)) - where occ = occName n +to_ie_post_rn (L l n) = L l (IEName noExtField ns_spec (L (l2l l) n)) + where + occ = occName n + ns_spec + | isTcOcc occ && isSymOcc occ = TypeNamespaceSpecifier noAnn + | otherwise = NoNamespaceSpecifier noExtField {- Note [Do not warn about Prelude hiding] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ===================================== compiler/GHC/Tc/Errors/Ppr.hs ===================================== @@ -697,12 +697,10 @@ instance Diagnostic TcRnMessage where child_name_fs = occNameFS (rdrNameOcc child_name) suggest_patsyn = allow_patsyn && could_be_patsyn could_be_patsyn = - case unLoc wname of - IEName{} -> isLexCon child_name_fs - IEData{} -> isLexCon child_name_fs - IEPattern{} -> True - IEType{} -> False - IEDefault{} -> False + case ieWrappedNamespaceSpecifier (unLoc wname) of + NoNamespaceSpecifier{} -> isLexCon child_name_fs + DataNamespaceSpecifier{} -> isLexCon child_name_fs + TypeNamespaceSpecifier{} -> False basic_msg = what_parent <+> quotes (ppr parent_name) <+> "does not define a child named" <+> quotes (ppr child_name) @@ -3385,12 +3383,10 @@ instance Diagnostic TcRnMessage where BadImportAvailTyCon -- The three cases (TyOp, DataKw, PatternKw) are laid out -- in Note [Reasons for BadImportAvailTyCon] in GHC.Tc.Errors.Types - | isSymOcc occ -> could_change_item ImportItemAddType -- Case (TyOp) - | otherwise -> -- Non-operator cases - case unLoc (ieLIEWrappedName ie) of - IEData{} -> could_change_item ImportItemRemoveData -- Case (DataKw) - IEPattern{} -> could_change_item ImportItemRemovePattern -- Case (PatternKw) - _ -> panic "diagnosticHints: unexpected BadImportAvailTyCon" + | isSymOcc occ -> could_change_item ImportItemAddType -- Case (TyOp) + | iePatternNameSpec ie -> could_change_item ImportItemRemovePattern -- Case (PatternKw) + | ieDataNameSpec ie -> could_change_item ImportItemRemoveData -- Case (DataKw) + | otherwise -> panic "diagnosticHints: unexpected BadImportAvailTyCon" BadImportAvailDataCon par -> [ ImportSuggestion occ $ ===================================== compiler/GHC/Tc/Gen/Export.hs ===================================== @@ -228,7 +228,8 @@ rnExports explicit_mod exports | explicit_mod = exports | has_main = Just [noLocA (IEVar Nothing - (noLocA (IEName noExtField $ noLocA default_main)) Nothing)] + (noLocA (IEName noExtField (NoNamespaceSpecifier noExtField) $ + noLocA default_main)) Nothing)] -- ToDo: the 'noLoc' here is unhelpful if 'main' -- turns out to be out of scope | otherwise = Nothing @@ -286,7 +287,7 @@ declarations, and neither does the re-export of a whole imported module. The export item `default IsString` is parsed into the `IE` item - IEThingAbs ext (L loc (IEDefault ext "IsString")) doc + IEDefault ext "IsString" If exported, a default is imported automatically much like a class instance. For example, @@ -532,65 +533,25 @@ exports_from_avail (Just rdr_items) rdr_env imports this_mod ------------- lookup_ie :: ExportAccum -> LIE GhcPs -> RnM (Maybe (ExportAccum, LIE GhcRn, Maybe AvailInfo)) + lookup_ie expacc (L loc ie@(IEVar warn_txt_ps l doc)) = + lookup_ie_var expacc loc ie warn_txt_ps what_look lrdr doc $ + \warn_txt_rn name doc' -> IEVar warn_txt_rn (replaceLWrappedName l name) doc' + where { lrdr = ieLWrappedName l + ; what_look = ieLWrappedNameWhatLooking l } + lookup_ie expacc (L loc ie@(IEPattern (warn_txt_ps, _) lrdr doc)) = + lookup_ie_var expacc loc ie warn_txt_ps WL_ConLike lrdr doc $ + \warn_txt_rn name doc' -> IEPattern warn_txt_rn (L (getLoc lrdr) name) doc' lookup_ie expacc@ExportAccum{ expacc_exp_occs = occs, expacc_warn_spans = export_warn_spans, expacc_dont_warn = dont_warn_export - } (L loc ie@(IEVar warn_txt_ps l doc)) - = do mb_gre <- lookupGreAvailRn (ieLWrappedNameWhatLooking l) $ lieWrappedName l - for mb_gre $ \ gre -> do - let avail = availFromGRE gre - name = greName gre - - checkThLocalNameNoLift $ ieLWrappedUserRdrName l gre - occs' <- check_occs occs ie [gre] - (export_warn_spans', dont_warn_export', warn_txt_rn) - <- process_warning export_warn_spans - dont_warn_export - [name] - warn_txt_ps - (locA loc) - - doc' <- traverse rnLHsDoc doc - return ( expacc{ expacc_exp_occs = occs' - , expacc_warn_spans = export_warn_spans' - , expacc_dont_warn = dont_warn_export' } - , L loc (IEVar warn_txt_rn (replaceLWrappedName l name) doc') - , Just avail ) - - lookup_ie expacc@ExportAccum{ - expacc_exp_occs = occs, - expacc_exp_dflts = exp_dflts, - expacc_warn_spans = export_warn_spans, - expacc_dont_warn = dont_warn_export } (L loc ie@(IEThingAbs warn_txt_ps l doc)) = do mb_gre <- lookupGreAvailRn (ieLWrappedNameWhatLooking l) $ lieWrappedName l for mb_gre $ \ gre -> do let avail = availFromGRE gre name = greName gre - (mb_avail, occs', exp_dflts') <- - case unLoc l of - -- see Note [Default exports] - IEDefault {} -> do - defaults <- tcg_default <$> getGblEnv - case lookupUFM_Directly defaults (nameUnique name) of - Nothing -> - do addErr $ TcRnExportHiddenDefault ie - return (Nothing, occs, exp_dflts) - Just cls_dflts -> do - let cls = cd_class cls_dflts - case lookupNameEnv exp_dflts (className cls) of - Just (_, ie') -> do - addDiagnostic $ - TcRnDuplicateNamedDefaultExport (classTyCon cls) ie ie' - return (Nothing, occs, exp_dflts) - Nothing -> - return $ (Nothing, occs, extendNameEnv exp_dflts (className cls) (cls_dflts, ie)) - _ -> do - occs' <- check_occs occs ie [gre] - return (Just avail, occs', exp_dflts) - + occs' <- check_occs occs ie [gre] checkThLocalNameNoLift (ieLWrappedUserRdrName l gre) (export_warn_spans', dont_warn_export', warn_txt_rn) <- process_warning export_warn_spans @@ -601,11 +562,10 @@ exports_from_avail (Just rdr_items) rdr_env imports this_mod doc' <- traverse rnLHsDoc doc return ( expacc{ expacc_exp_occs = occs' - , expacc_exp_dflts = exp_dflts' , expacc_warn_spans = export_warn_spans' , expacc_dont_warn = dont_warn_export' } , L loc (IEThingAbs warn_txt_rn (replaceLWrappedName l name) doc') - , mb_avail ) + , Just avail ) lookup_ie expacc@ExportAccum{ expacc_exp_occs = occs, @@ -674,8 +634,74 @@ exports_from_avail (Just rdr_items) rdr_env imports this_mod , L loc (IEThingWith (warn_txt_rn, ann) (replaceLWrappedName l name) wc subs doc') , Just $ AvailTC name all_names ) + lookup_ie expacc@ExportAccum{ + expacc_exp_dflts = exp_dflts, + expacc_warn_spans = export_warn_spans, + expacc_dont_warn = dont_warn_export + } (L loc ie@(IEDefault (warn_txt_ps, _) (L lrdr rdr))) + -- see Note [Default exports] + = do mb_gre <- lookupGreAvailRn WL_TyCon rdr + for mb_gre $ \ gre -> do + let name = greName gre + + defaults <- tcg_default <$> getGblEnv + exp_dflts' <- do + case lookupUFM_Directly defaults (nameUnique name) of + Nothing -> + do addErr $ TcRnExportHiddenDefault ie + return exp_dflts + Just cls_dflts -> do + let cls = cd_class cls_dflts + case lookupNameEnv exp_dflts (className cls) of + Just (_, ie') -> do + addDiagnostic $ + TcRnDuplicateNamedDefaultExport (classTyCon cls) ie ie' + return exp_dflts + Nothing -> + return $ (extendNameEnv exp_dflts (className cls) (cls_dflts, ie)) + + checkThLocalNameNoLift (L lrdr (WithUserRdr rdr gre)) + (export_warn_spans', dont_warn_export', warn_txt_rn) + <- process_warning export_warn_spans + dont_warn_export + [name] + warn_txt_ps + (locA loc) + + return ( expacc{ expacc_exp_dflts = exp_dflts' + , expacc_warn_spans = export_warn_spans' + , expacc_dont_warn = dont_warn_export' } + , L loc (IEDefault warn_txt_rn (L lrdr name)) + , Nothing ) + lookup_ie _ _ = panic "lookup_ie" -- Other cases covered earlier + -- Common logic for IEVar and IEPattern in lookup_ie above + lookup_ie_var expacc@ExportAccum{ + expacc_exp_occs = occs, + expacc_warn_spans = export_warn_spans, + expacc_dont_warn = dont_warn_export + } loc ie warn_txt_ps what_look lrdr doc mk_ie + = do mb_gre <- lookupGreAvailRn what_look $ unLoc lrdr + for mb_gre $ \ gre -> do + let avail = availFromGRE gre + name = greName gre + + checkThLocalNameNoLift $ fmap @LocatedN (\rdr -> WithUserRdr rdr gre) lrdr + occs' <- check_occs occs ie [gre] + (export_warn_spans', dont_warn_export', warn_txt_rn) + <- process_warning export_warn_spans + dont_warn_export + [name] + warn_txt_ps + (locA loc) + + doc' <- traverse rnLHsDoc doc + return ( expacc{ expacc_exp_occs = occs' + , expacc_warn_spans = export_warn_spans' + , expacc_dont_warn = dont_warn_export' } + , L loc (mk_ie warn_txt_rn name doc') + , Just avail ) lookup_ie_kids_with :: GlobalRdrElt -> [LIEWrappedName GhcPs] -> RnM ([LIEWrappedName GhcRn], [GlobalRdrElt]) @@ -802,12 +828,11 @@ ieLWrappedUserRdrName l n = (\rdr -> WithUserRdr rdr n) <$> ieLWrappedName l -- | In what namespaces should we go looking for an import/export item -- that is out of scope, for suggestions in error messages? ieWrappedNameWhatLooking :: IEWrappedName GhcPs -> WhatLooking -ieWrappedNameWhatLooking = \case - IEName {} -> WL_TyCon_or_TermVar - IEDefault {} -> WL_TyCon - IEType {} -> WL_Type - IEData {} -> WL_Term - IEPattern {} -> WL_ConLike +ieWrappedNameWhatLooking wname = + case ieWrappedNamespaceSpecifier wname of + NoNamespaceSpecifier {} -> WL_TyCon_or_TermVar + TypeNamespaceSpecifier {} -> WL_Type + DataNamespaceSpecifier {} -> WL_Term ieLWrappedNameWhatLooking :: LIEWrappedName GhcPs -> WhatLooking ieLWrappedNameWhatLooking = ieWrappedNameWhatLooking . unLoc @@ -884,9 +909,9 @@ lookupChildrenExport parent_gre child_gres rdr_items = mapAndReportM doOne rdr_i -> RnM (LIEWrappedName GhcRn, GlobalRdrElt) doOne n = do - let all_ns = case unLoc n of - IEName{} -> True -- Ignore the namespace iff the name is unadorned - _ -> False + let all_ns = case ieWrappedNamespaceSpecifier (unLoc n) of + NoNamespaceSpecifier{} -> True -- Ignore the namespace iff the name is unadorned + _ -> False let bareName = lieWrappedName n -- Do not report export list declaration deprecations name <- lookupSubBndrOcc_helper False all_ns ExportDeprecationWarnings @@ -1114,6 +1139,7 @@ dupExport_ok child ie1 ie2 single IEVar {} = True single IEThingAbs {} = True + single IEPattern {} = True single _ = False failWithDcErr :: Name -> Name -> [Name] -> TcM a ===================================== compiler/Language/Haskell/Syntax/Extension.hs ===================================== @@ -822,8 +822,6 @@ type family XXIE x type family XIEName p type family XIEDefault p type family XIEPattern p -type family XIEType p -type family XIEData p type family XXIEWrappedName p -- ------------------------------------- ===================================== compiler/Language/Haskell/Syntax/ImpExp.hs ===================================== @@ -124,6 +124,25 @@ data IE pass -- module Mod ( Test(f, g) ) -- import Mod ( Test(f, g) ) -- @ + | IEDefault (XIEDefault pass) (LIdP pass) + -- ^ Export of named defaults for a given class. Can only occur in export list. + -- + -- @ + -- module Mod ( default Cls ) + -- @ + -- + -- See Note [Named default declarations] in GHC.Tc.Gen.Default + | IEPattern (XIEPattern pass) (LIdP pass) (Maybe (ExportDoc pass)) + -- ^ Legacy syntax to import or export a pattern synonym or a data + -- constructor using the @pattern@ namespace specifier. + -- + -- @ + -- module Mod ( pattern P ) + -- import Mod ( pattern P ) + -- @ + -- + -- This form is deprecated in favour of the @data@ namespace specifier. + -- Its use is reported by @-Wpattern-namespace-specifier@. | IEModuleContents (XIEModuleContents pass) (XRec pass HsModuleName) -- ^ Export of entire module. Can only occur in export list. -- @@ -180,11 +199,9 @@ data IEWildcard -- adornments. Used primarily for accurate pretty printing of -- ParsedSource, and API Annotation placement. data IEWrappedName p - = IEName (XIEName p) (LIdP p) -- ^ unadorned name, e.g @myFun@ - | IEDefault (XIEDefault p) (LIdP p) -- ^ @default X ()@, see Note [Named default declarations] in GHC.Tc.Gen.Default - | IEPattern (XIEPattern p) (LIdP p) -- ^ @pattern X@ - | IEType (XIEType p) (LIdP p) -- ^ @type (:+:)@ - | IEData (XIEData p) (LIdP p) -- ^ @data (:+:)@ + = IEName (XIEName p) (NamespaceSpecifier p) (LIdP p) + -- ^ A name with an optional namespace specifier, + -- e.g. @myFun@, @type (:+:)@, @data P@. | XIEWrappedName !(XXIEWrappedName p) -- | Located name with possible adornment ===================================== testsuite/tests/haddock/should_compile_flag_haddock/T17544_kw.stderr ===================================== @@ -65,6 +65,8 @@ [])) (IEName (NoExtField) + (NoNamespaceSpecifier + (NoExtField)) (L (EpAnn (EpaSpan { T17544_kw.hs:9:3-5 }) @@ -100,6 +102,8 @@ [])) (IEName (NoExtField) + (NoNamespaceSpecifier + (NoExtField)) (L (EpAnn (EpaSpan { T17544_kw.hs:10:3-5 }) @@ -133,6 +137,8 @@ [])) (IEName (NoExtField) + (NoNamespaceSpecifier + (NoExtField)) (L (EpAnn (EpaSpan { T17544_kw.hs:11:3-5 }) ===================================== testsuite/tests/parser/should_compile/DumpRenamedAst.stderr ===================================== @@ -2903,6 +2903,8 @@ [])) (IEName (NoExtField) + (NoNamespaceSpecifier + (NoExtField)) (L (EpAnn (EpaSpan { DumpRenamedAst.hs:7:19-22 }) ===================================== testsuite/tests/parser/should_compile/T14189.stderr ===================================== @@ -360,6 +360,8 @@ [])) (IEName (NoExtField) + (NoNamespaceSpecifier + (NoExtField)) (L (EpAnn (EpaSpan { T14189.hs:3:3-8 }) @@ -379,6 +381,8 @@ [])) (IEName (NoExtField) + (NoNamespaceSpecifier + (NoExtField)) (L (EpAnn (EpaSpan { T14189.hs:3:11 }) @@ -395,6 +399,8 @@ [])) (IEName (NoExtField) + (NoNamespaceSpecifier + (NoExtField)) (L (EpAnn (EpaSpan { T14189.hs:3:13-14 }) ===================================== utils/check-exact/ExactPrint.hs ===================================== @@ -2059,7 +2059,7 @@ instance ExactPrint (WarnDecl GhcPs) where exact (Warning (o,c) ns_spec lns (WarningTxt src mb_cat ls )) = do mb_cat' <- markAnnotated mb_cat - ns_spec' <- exactNsSpec ns_spec + ns_spec' <- markAnnotated ns_spec lns' <- mapM markAnnotated lns o' <- markEpToken o ls' <- mapM markAnnotated ls @@ -2067,22 +2067,13 @@ instance ExactPrint (WarnDecl GhcPs) where return (Warning (o',c') ns_spec' lns' (WarningTxt src mb_cat' ls')) exact (Warning (o,c) ns_spec lns (DeprecatedTxt src ls)) = do - ns_spec' <- exactNsSpec ns_spec + ns_spec' <- markAnnotated ns_spec lns' <- mapM markAnnotated lns o' <- markEpToken o ls' <- mapM markAnnotated ls c' <- markEpToken c return (Warning (o',c') ns_spec' lns' (DeprecatedTxt src ls')) -exactNsSpec :: (Monad m, Monoid w) => NamespaceSpecifier GhcPs -> EP w m (NamespaceSpecifier GhcPs) -exactNsSpec (NoNamespaceSpecifier x) = pure (NoNamespaceSpecifier x) -exactNsSpec (TypeNamespaceSpecifier type_) = do - type_' <- markEpToken type_ - pure (TypeNamespaceSpecifier type_') -exactNsSpec (DataNamespaceSpecifier data_) = do - data_' <- markEpToken data_ - pure (DataNamespaceSpecifier data_') - -- --------------------------------------------------------------------- instance ExactPrint (StringLiteral GhcPs) where @@ -4719,6 +4710,19 @@ instance ExactPrint (IE GhcPs) where doc' <- markAnnotated doc return (IEThingWith (depr', (op',dd',c',cp')) thing' wc' withs' doc') + exact (IEDefault (depr, r) n) = do + depr' <- markAnnotated depr + r' <- markEpToken r + n' <- markAnnotated n + return (IEDefault (depr', r') n') + + exact (IEPattern (depr, r) n doc) = do + depr' <- markAnnotated depr + r' <- markEpToken r + n' <- markAnnotated n + doc' <- markAnnotated doc + return (IEPattern (depr', r') n' doc') + exact (IEModuleContents (depr, an) m) = do depr' <- markAnnotated depr an0 <- markEpToken an @@ -4746,25 +4750,10 @@ instance ExactPrint (IEWrappedName GhcPs) where getAnnotationEntry = const NoEntryVal setAnnotationAnchor a _ _ _ = a - exact (IEName x n) = do - n' <- markAnnotated n - return (IEName x n') - exact (IEDefault r n) = do - r' <- markEpToken r - n' <- markAnnotated n - return (IEDefault r' n') - exact (IEPattern r n) = do - r' <- markEpToken r - n' <- markAnnotated n - return (IEPattern r' n') - exact (IEType r n) = do - r' <- markEpToken r - n' <- markAnnotated n - return (IEType r' n') - exact (IEData r n) = do - r' <- markEpToken r + exact (IEName x ns_spec n) = do + ns_spec' <- markAnnotated ns_spec n' <- markAnnotated n - return (IEData r' n') + return (IEName x ns_spec' n') -- --------------------------------------------------------------------- ===================================== utils/check-exact/Main.hs ===================================== @@ -865,8 +865,9 @@ addHiding1 _libdir (L l p) = do [L li imp1,imp2] = hsmodImports p n1 = L noAnnSrcSpanDP0 (mkVarUnqual (mkFastString "n1")) n2 = L noAnnSrcSpanDP0 (mkVarUnqual (mkFastString "n2")) - v1 = L (addComma $ noAnnSrcSpanDP0) (IEVar Nothing (L noAnnSrcSpanDP0 (IEName noExtField n1)) Nothing) - v2 = L ( noAnnSrcSpanDP0) (IEVar Nothing (L noAnnSrcSpanDP0 (IEName noExtField n2)) Nothing) + v1 = L (addComma $ noAnnSrcSpanDP0) (IEVar Nothing (L noAnnSrcSpanDP0 (IEName noExtField ns_spec n1)) Nothing) + v2 = L ( noAnnSrcSpanDP0) (IEVar Nothing (L noAnnSrcSpanDP0 (IEName noExtField ns_spec n2)) Nothing) + ns_spec = NoNamespaceSpecifier noExtField impHiding = [v1,v2] impAnnList = (EpTok d1, EpTok d1, EpTok d0, []) imp1' = imp1 { ideclExt @@ -892,8 +893,9 @@ addHiding2 _libdir top = do Just (_, ns) = ideclImportList imp1 n1 = L (noAnnSrcSpanDP0) (mkVarUnqual (mkFastString "n1")) n2 = L (noAnnSrcSpanDP0) (mkVarUnqual (mkFastString "n2")) - v1 = L (addComma $ noAnnSrcSpanDP0) (IEVar Nothing (L noAnnSrcSpanDP0 (IEName noExtField n1)) Nothing) - v2 = L ( noAnnSrcSpanDP0) (IEVar Nothing (L noAnnSrcSpanDP0 (IEName noExtField n2)) Nothing) + v1 = L (addComma $ noAnnSrcSpanDP0) (IEVar Nothing (L noAnnSrcSpanDP0 (IEName noExtField ns_spec n1)) Nothing) + v2 = L ( noAnnSrcSpanDP0) (IEVar Nothing (L noAnnSrcSpanDP0 (IEName noExtField ns_spec n2)) Nothing) + ns_spec = NoNamespaceSpecifier noExtField L ln n = last ns n' = L (addComma ln) n impAnnList = (EpTok d1, EpTok d1, EpTok d0, []) View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/08166c5be2f2d8ec2cfeced666a3dc4f... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/08166c5be2f2d8ec2cfeced666a3dc4f... You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help
participants (1)
-
Vladislav Zavialov (@int-index)