-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix marking of toplevel attributes in implementations #13170
Conversation
There are some failures in the testsuite that seem wrong; I'll take a look tomorrow. |
To be figured out whether this is related to #13155 or not.
Hope to be able to provide feedback on this later.
|
I think the problem may be not (just) with warning 53, but also with top-level alert attributes themselves. Locally with two files: a.ml: [@@@alert unstable "foo"]
let x = 42 b.ml: let x = A.x And running:
I find that on OCaml 4.14.2, I get an alert when compiling the second file, but on OCaml 5.0.0 (and later) I do not. So the warning 53 implementation here seems "correct" with respect to the behavior of the alert attribute in this particular program, but the attribute itself is not behaving as promised in the manual since OCaml 5. I have not investigated what caused this behavior change. |
I think this is not related to #13155 - I'm reasonably confident the bug there is just that the driver for the |
Awesome! Thanks!
|
I think you are missing an
|
🤦♂️ Indeed. |
I pushed a different (and this time, hopefully correect) fix. The key point as I understand it is that floating alerts |
b1854e4
to
d4fbd86
Compare
Yes - will do. By the way, I still don't quite understand the alert behavior that confused me above. Using the versions of the compiler in opam, it is the case that to get the alert in my example the Do you know if this change was intentional? Looking quickly, I don't think the manual says one way or another whether we should expect alerts to be enabled by default. But it seems surprising to me that they are not. |
I think your new test test shows the behavior of w53 is not quite right yet. The attributes in the This becomes clearer if you rename those alerts to be different from the ones at the level of the compilation unit. You will see that line 13 in According to the manual, this behavior of the alert attributes is expected (at least, my understanding from the mention of I can imagine two possible fixes:
But actually I think that will have a different bug. The manual says you can only write compilation-unit-level alert attributes in an
|
Alerts are enabled by default, except Line 868 in 51e4d9a
|
Thanks for the quick review! You are right, I was also puzzled as to what the exact semantics of alerts across compilation units should be and so the patch is not quite right yet. I will mull over your message but here is a firs set of remarks.
Yes, sorry for the confusion, I was thinking of the
In my mind, not looking at the |
I agree this is confusing! Just testing, it seems that
I agree: A floating alert should not trigger warning 53 if it is:
My comment about |
I pushed a new version of the PR which following some of the suggestions of @ccasin. Rather than trying to propagate enough information to the typechecking routine to be able to mark floating alerts accurately, we delay their marking until after typechecking, when the information is naturally available. Concretely, for both interfaces and implementations, we now:
Comments welcome! Thanks. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the changes, I think this came out very well!
It would be nice to have a test showing a top-level alert attribute in an ml will trigger warning 53 if there is an mli, to document this slightly subtle behavior. But I did confirm this works locally, and I don't think the PR needs to be held up for such a test.
Separately, thinking about the interaction of the cmi behavior with various flags made me realize that we'll issue w53 for alert attributes if, for example, the user passes -i
or -stop-after parsing
. But actually this is already a problem for all attributes, and not made worse by this PR, so again I do not think this PR needs to fix it. I will look at making a PR for that.
17fd561
to
157aa17
Compare
Thanks for the review. I pushed an additional test checking that warning is correctly triggered across |
This is ready to go from my side. In order to respect the protocol, this PR would need an approval from another core dev before it can be merged. |
typing/typemod.ml
Outdated
@@ -1706,7 +1706,7 @@ and transl_signature ?(toplevel = false) env sg = | |||
typedtree, sg, final_env | |||
| Psig_attribute x -> | |||
Builtin_attributes.warning_attribute x; | |||
if toplevel || not (Warnings.is_active (Misplaced_attribute "")) | |||
if not (Warnings.is_active (Misplaced_attribute "")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Discarding attributes when they appear in a parsetree location where the warning 53
is disabled sounds useful in general. Maybe we could have a filter
function in Buitin_attribute
? (and extend its use later on). Moreover, since the new code is not dependent on the toplevel argument, would it make sense to move it to the warning_attribute
function ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea bout merging this logic inside warning_attribute
; done in 73c8720. This fixes yet another small bug: in the following program,
class c =
object
[@@@warning "-53"]
[@@@alert foo "foo"]
end
the alert would trigger Warning 53 even though it the warning is supposed to be disabled. After moving the logic to warning_attribute
, the warning is no longer triggered.
I added a few more tests to exhibit this behaviour.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall, I agree that this is a nice if subtle fix. Moreover, the fix makes the correctness of the code easier to check by moving the code marking the alerts closer to the consumer of the alerts which is Env.save_signature
.
My remarks are minor and can be safely ignored (or delayed to a later PR).
Thanks for the review @Octachron! Am planning to merge the the PR once the CI passes. |
There seems to be a small regression in 5.2 (probably due to #12451, cc @ccasin): top-level attributes in implementations are not correctly marked as "used".