Master_T
Master_T

Reputation: 7913

Ada compiler: warning variable <X> is assigned but never read

I have the following very simple piece of code in Ada which is giving me grief. I trimmed down the code to the minimum to show the problem, the only thing you need to know is that Some_Task is a task type:

task body TB is
    Task1 : Some_Task_Ref;
begin
   Task1 := new Some_Task;
    loop
         Put_Line("Main loop is running, whatever...");
         delay 5.0;
    end loop;
end TB;

From what I understand about task activation in Ada this should be sufficient: I'm creating a task of type "Some_Task" and I don't have to do anything with it, it will execute it's main loop without any intervention. It's not like in java where you have to call a "start" method on the task object.

But if I'm correct, why is the compiler refusing to build, giving me the error:

warning variable "Task1" is assigned but never read

Why should I be forced to "read" Task1? It's a task, all it needs to do is run... what am I missing?

Note: this seems to happen only when I use GNAT in "Gnat mode" (switch -gnatg). Unfortunately I need this mode for some advanced pragmas, but it seems it introduces some "overzelous" checks like the one causing the problem above. How can I deactivate that check?

Upvotes: 0

Views: 1723

Answers (2)

Master_T
Master_T

Reputation: 7913

Just to answer this question, since the answer was posted in a comment, which cannot be marked as an answer.

As Holt said (all props to him) this can be fixed by using:

pragma Warnings (Off, Some_Task_Ref) ;

Upvotes: 0

egilhh
egilhh

Reputation: 6430

It's a warning, not an error, and does not prevent building an executable (unless you've turned on "treat warnings as errors"). It's a hint from the compiler that you may have made a mistake in creating a variable that is never used. You can tell the compiler that you don't indend to use Task1 by declaring it as a constant, like this:

Task1 : constant Some_Task_Ref := new Some_Task;

Upvotes: 2

Related Questions