Verified Commit 28550d45 authored by Camil Staps's avatar Camil Staps 🚀

Add warnings for types used with an unexpected number of arguments to backend (#226)

parent 709cdd7d
Subproject commit 3889482ae0b805f4b9e2961c0e202ba4a1177d8e
Subproject commit f0cb0ca4e6afa27a6976ad6fefa6e7ecee558f8b
......@@ -23,7 +23,7 @@ import Control.Monad
import Data.Error
from Data.Func import $, hyperstrict, instance Functor ((->) r), mapSt, seqSt, on, `on`
import Data.Functor
from Data.List import permutations
from Data.List import intersperse, permutations
import Data.Maybe
import Data.Tuple
import System._Posix
......@@ -251,6 +251,7 @@ handle (Just request=:{unify,name,page}) db w
= respond start Nothing (err InvalidType "Couldn't parse type" Nothing) db w
| all isNothing [unify,name,request.exactName,request.typeName,request.className] && isNothing request.using
= respond start Nothing (err InvalidInput "Empty query" Nothing) db w
#! (warnings,db) = getWarnings (parseType =<< fromString <$> unify) db
// Results
#! drop_n = fromJust (page <|> pure 0) * MAX_RESULTS
#! (res,suggs,db) = searchWithSuggestions request db
......@@ -271,6 +272,7 @@ handle (Just request=:{unify,name,page}) db w
& data = results
, more_available = Just more
, suggestions = suggs
, warnings = if (isEmpty warnings) Nothing (Just $ removeDup warnings)
}
// Save page prefetches
#! w = cachePages key CACHE_PREFETCH 1 response nextpages w
......@@ -287,6 +289,27 @@ where
(True,Just k) -> writeCache LongTerm k r w
_ -> w)
getWarnings :: !(Maybe Type) !*CloogleDB -> (![String], !*CloogleDB)
getWarnings Nothing db = ([], db)
getWarnings (Just t) db
# (warnings,db) = mapSt checkTypeVariables [(n,length args) \\ type=:(Type n args) <- subtypes t] db
= (catMaybes warnings,db)
where
checkTypeVariables :: !(!Name,!Int) !*CloogleDB -> *(!Maybe String, !*CloogleDB)
checkTypeVariables (name,nargs) db
# (entries,db) = getExactNameMatches name db
# nvars = sort $ removeDup [length tde_typedef.td_args \\ TypeDefEntry {tde_typedef} <|- entries]
| isEmpty nvars
= (Nothing,db)
| isMember nargs nvars
= (Nothing,db)
= case nvars of
[1] -> (Just (concat $ warn ++ ["1 was expected"]),db)
[n] -> (Just (concat $ warn ++ [toString n," were expected"]),db)
_ -> (Just (concat $ warn ++ intersperse ", " (map toString (init nvars)) ++ [", or ",toString (last nvars)," were expected"]),db)
where
warn = ["Type {{`",name,"`}} used with ",toString nargs," argument",if (nargs==1) "" "s"," where "]
cachePages :: !RequestCacheKey !Int !Int !Response ![Result] !*World -> *World
cachePages key _ _ _ [] w = w
cachePages key 0 _ _ _ w = w
......@@ -426,9 +449,8 @@ where
err :: !CloogleError !String !(Maybe [(Request,Int)]) -> Response
err c m suggs =
{ return = toInt c
, data = []
{ zero
& return = toInt c
, msg = m
, more_available = Nothing
, suggestions = suggs
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment