Block comment syntax is currently not omnipotent. There are two character sequences that can't be put into a comment: /*
and */
. The former starts a (surprising) nested comment. The latter of course terminates the comment. But both can have other meanings one may want to comment about, e.g. a Shell wildcard and other examples in the 2nd answer.
Illegal syntax can pop up, when wrapping a block comment around valid code containing /*
or */
in a string or line end comment or in a procedural macro, with some totally different syntax (see cron!
below).
To remedy this I suggest not touching existing comment syntax, but an additional raw, non-nesting comment syntax. As with raw strings, the number of hashes has to be at least one more than any apparent contained comment end. So each of these lines are a single comment:
r#/* … */ … /* … r#/* … */#
r##/* … */# … /* … r##/* … */##
Being a syntax error, r#/*
doesn't seem to collide with anything currently possible. Whereas r/*
could be r
followed by a comment, so at least one hash must be mandatory, like it is for raw identifiers.
This would work for all three comment types, i.e. also r#/**
and r#/*!
.
They are non-nesting in the sence that there is no magic as with /* /* nested */ */
. Only a close with as many hashes ends the raw coment r#/* r#/* not nested */#
. That means with enough hashes you can put anything inside, including other classic and raw comments.
If we want these to nest within classical block comments, the 1st line would be one comment, not seeing the closing within the raw comment. But such a complication is not necessary, as any raw comment can instead be embedded in another with more hashes (2nd line.)
/* … r#/* … */ … */# … */
r##/* … r#/* … */ … */# … */##
Two scenarios hint at accidental /*
or */
as comment content:
- a runaway comment containing a nested comment
- an orphaned comment end after a block comment, except in a macro that allows it (see
cron!
answer)
The compiler should suggest raw syntax, when these happen.