Uh oh!
There was an error while loading. Please reload this page.
copilot-language's duplicate extern check doesn't work correctly for locals
#751
RyanGlScott
started this conversation in
General
Replies: 0 comments
Sign up for freeto join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Consider this Copilot spec:
{-# LANGUAGE DataKinds #-} {-# LANGUAGE NoImplicitPrelude #-} moduleMain (main) whereimportCopilot.Compile.C99importLanguage.Copilots::StreamWord64 s = extern @(Array2Word64) "x"Nothing! extern @Word32"x"Nothingspec::Spec spec = trigger "trig" true [arg s] main::IO() main =do spec' <- reify spec compile "bug" spec'The definition of
sis erroneous, as it declares two externsxwith distinct types (Array 2 Word64andWord32). If you attempt to reify this,copilot-language's analysis will detect this and raise an error:So far, so good. Now consider what happens if you use this slightly modified definition of
s:This time, the first extern (of type
Array 2 Word64) has been bound usinglocal, but the second extern (of typeWord32) has not. Copilot will happily compile this version of the spec to C code:This C code is very wrong, however, as it attempts to use
local_0(of typeuint64_t*) as an array index, which is ill-typed:Why does Copilot detect the duplicate externs in the first version of the program but not the second? It's because of how
copilot-languageimplements this check:copilot/copilot-language/src/Copilot/Language/Analyze.hs
Line 330 in 4e5fd9a
This checks
e(the first argument tolocal) for duplicate externs, but it does not check the result of the higher-order function (the second argument tolocal). To do this check correctly, we'd need to look at both. One way to do so would be to mirror another check elsewhere incopilot-language, which passes a dummy variable to the higher-order function in order to evaluate it:copilot/copilot-language/src/Copilot/Language/Analyze.hs
Lines 144 to 145 in 4e5fd9a
All reactions