I agree that it can be useful. (Though, unfortunately, this would complicate the implementation again. Currently `unused' attributes are handled by just marking the thing as used. For this warning we'd need another flag and extra code to handle the two flags. It's all more work than I'd like to spend on the matter, at least for now ...)
Fair enough - I just wanted to make sure the idea was known and considered since it might be the case that it fell out easily and if so would certainly be worth implementing.
But now that you wrote the example, I see a syntactic problem. We already have the syntax `<type> <attribute>' for type attributes. Though we don't allow type attributes in parameters (more or less in the spirit of Pascal's forbidding new structured types there), I wouldn't like to have the same syntax mean something different here.
For variable's attributes, we have `var <name> = <type>; <attribute>'. But wouldn't this look strange in a parameter list?
function doit( a: Integer; b: Integer; attribute (unused) ): Integer;
This would at first glance look like another parameter (though syntactically invalid, even with procedural parameters and all the extensions we have, AFAICS, so maybe not a conflict, but still strange) ...
Yes, strange indeed.
A related question is if we should simply allow `unused' (and perhaps more) as type attributes. I.e., we'd create a variant of a type which is generally unused. This sounds strange, of course. I'm reminded of this, because I've once wondered if we should have `static' as a type attribute (though I'm not sure yet if I'd really need it, probably not) ...
Also strange, and quite cumbersome I would think for any particular case.
Obviously, the easiest thing for me would be just to implement the {$unused(b)} directive,
I think this would be quite a bit more difficult, and raises some new questions, since directives are outside of the syntax rules. E.g.:
procedure foo (a: Integer);
{$unused(a)}
procedure bar (a: Integer); begin end;
begin end;
Which `a' is meant here? The outer one? What if you move the directive after the word `procedure', is it the inner one now? If not, it would seem confusing. If so, it would need to reference an identifier before it's declared, also bad ...
Basically I guess since it is accessing the symbol table it would apply to the same thing the compiler currently considered as the "current in scope variable", which would be ambiguous at best everywhere from "procedure" up to the ";" (and possibly one more token. For myself (and we could certainly recommend it as the way to go), I generally write the {$unused(a,b,c)} line on the first line after the begin. That ensures the compiler and the parser are pretty much guaranteed to be in sync.
Enjoy, Peter.