You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
**Description**
The current implementation of Copilot does not allow using the same
trigger handler name multiple times, since the C99 backend produces
multiple repeated declarations, which is invalid.
So long as the types do not change, or so long as the target language
allows for method/function overloading, then the Copilot spec should
accept them.
**Type**
- Feature: extend valid specifications to allow multiple triggers with
the same handler.
**Additional context**
None.
**Requester**
- Antanas Kalkauskas (Sensemtry).
**Method to check presence of bug**
Not applicable (not a bug).
**Expected result**
Copilot accepts specs where there are two triggers with the same name,
so long as the types of the arguments (and their arity) is the same.
The following dockerfile checks that a spec with the same trigger name
used multiple times can be compiled correctly, and the resulting C code
also compiles, after which it prints the message "Success":
```
--- Dockerfile-verify-296
FROM ubuntu:trusty
RUN apt-get update
RUN apt-get install --yes software-properties-common
RUN add-apt-repository ppa:hvr/ghc
RUN apt-get update
RUN apt-get install --yes ghc-8.6.5 cabal-install-2.4
RUN apt-get install --yes libz-dev
ENV PATH=/opt/ghc/8.6.5/bin:/opt/cabal/2.4/bin:$PWD/.cabal-sandbox/bin:$PATH
RUN cabal update
RUN cabal v1-sandbox init
RUN cabal v1-install alex happy
RUN apt-get install --yes git
ADD MultipleTriggers.hs /tmp/MultipleTriggers.hs
SHELL ["/bin/bash", "-c"]
CMD git clone $REPO && cd $NAME && git checkout $COMMIT && cd .. \
&& cabal v1-install $NAME/copilot**/ \
&& cabal v1-exec -- runhaskell /tmp/MultipleTriggers.hs \
&& gcc -c triggers.c \
&& echo "Success"
--- MultipleTriggers.hs
import Language.Copilot
import Copilot.Compile.C99
import Prelude hiding ((>), (<), div)
-- External temperature as a byte, range of -50C to 100C
temp :: Stream Word8
temp = extern "temperature" Nothing
spec = do
-- Triggers that fire when the temp is too low or too high, pass the current
-- temp as an argument.
trigger "adjust" (temp < 18) [arg temp]
trigger "adjust" (temp > 21) [arg temp]
-- Compile the spec
main = reify spec >>= compile "triggers"
```
Command (substitute variables based on new path after merge):
```
$ docker run -e "REPO=https://github.com/Copilot-Language/copilot" -e "NAME=copilot" -e "COMMIT=<HASH>" -it copilot-verify-296
```
**Solution implemented**
Introduce a new type to represent triggers with a unique name in the
generated C code. The unique trigger names are local to the C
implementation and are not visible in the generated C header file.
Modify implementation to use the unique triggers instead of the
pre-existing triggers, assigning them unique names.
Modify C header file generator to remove duplicate handler declarations.
Modify top-level checks run prior to code generation to check that
multiple triggers referring to the same handler pass arguments with the
same types in both cases (since C does not support function
polymorphism).
**Further notes**
None.
0 commit comments