-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Print the two types in the span label for transmute errors. #42304
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
src/librustc/middle/intrinsicck.rs
Outdated
format!("transmuting between {} and {}", | ||
skeleton_string(from, sk_from), | ||
skeleton_string(to, sk_to))) | ||
format!("transmuting between {} and {}", from, to)) |
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.
at minimum, this should be {}
and {}
-- but it definitely feels like we can do better. If nothing else, these error messages are really long, and I think it'd be better to break it up in parts. Let me see if I can draft a suggestion.
Maybe something like this?
|
cc @estebank |
For the general case, I feel something along the lines of this would be ok:
but I'd like to have a specific message for this case along the lines of
For example, in fn takes_u8(_: u8) {}
fn main() {
unsafe { takes_u8(::std::mem::transmute(0u16)); }
} I feel the current output is relatively ok:
and would look ridiculous under the above proposal:
Because of all of this, I would check for the case where there's an unknown layout in the source and target. If they are, present my second proposed output, otherwise, @nikomatsakis proposed output. If both source and target are not of unknown layout, either have a single-line note or (my preference) keep the current label. Part of the problem here is that this is two different errors conceptually, the compiler knows in the last example that the types have different sizes, while the other case they might be of different sizes, so they warrant slightly different output. |
Hm, okay. I can't say I entirely follow the discussion quite right now, but I'll look into implementing things in this way soon. |
@Mark-Simulacrum great let me know if you need anything |
@Mark-Simulacrum It'd also be great if you also moved these tests to |
fwiw, I don't think that final version looks particularly ridiculous. In fact, I maybe slightly prefer it. I think in particular I'd like to migrate towards a version where the "main error message" is fairly generic, and the details are exposed elsewhere. I think a lot of times we try to pack too much info into the header. But we really need to try and schedule some time to draw up some overall guidelines in this respect. But I'm also happy with a special case. Don't care that much. |
9ce7f5a
to
22f9b0e
Compare
src/librustc/middle/intrinsicck.rs
Outdated
@@ -92,7 +92,7 @@ impl<'a, 'gcx, 'tcx> ExprVisitor<'a, 'gcx, 'tcx> { | |||
struct_span_err!(self.infcx.tcx.sess, span, E0591, | |||
"`{}` is zero-sized and can't be transmuted to `{}`", | |||
from, to) | |||
.span_note(span, "cast with `as` to a pointer instead") |
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.
The span here was entirely not helpful as far as I can tell; but I'm open to re-adding it if there's disagreement.
@@ -111,7 +111,7 @@ impl<'a, 'gcx, 'tcx> ExprVisitor<'a, 'gcx, 'tcx> { | |||
} | |||
Err(LayoutError::Unknown(bad)) => { | |||
if bad == ty { | |||
format!("size can vary") |
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.
I'm not sure if there's much point to this branch myself; I can't come up with an example that would follow it.
| | ||
= help: cast with `as` to a pointer instead | ||
|
||
error[E0591]: `unsafe fn() -> (isize, *const (), std::option::Option<fn()>) {foo}` is zero-sized and can't be transmuted to `*const ()` |
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.
Should we change this error to also follow the same pattern of a simple header and notes to explain it?
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.
I think that would be best. The error text should be something like can't transmute zero-sized type
.
Updated; left a few comments, let me know what you think. |
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.
I'm ok with the output.
| | ||
= help: cast with `as` to a pointer instead | ||
|
||
error[E0591]: `unsafe fn() -> (isize, *const (), std::option::Option<fn()>) {foo}` is zero-sized and can't be transmuted to `*const ()` |
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.
I think that would be best. The error text should be something like can't transmute zero-sized type
.
22f9b0e
to
708bc6c
Compare
Updated the zero-sized case as well. |
src/librustc/middle/intrinsicck.rs
Outdated
format!("transmuting between {} and {}", | ||
skeleton_string(from, sk_from), | ||
skeleton_string(to, sk_to))) | ||
"transmute called with differently sized types") |
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.
Nit: "called with types of different sizes" sounds more grammatical to me
This looks good to me modulo the nit about the error message phrasing. |
708bc6c
to
e7b8935
Compare
@bors r=nikomatsakis Fixed the nit. |
📌 Commit e7b8935 has been approved by |
@bors rollup |
@bors r- I need to fix the UI tests to not be dependent on 32/64-bit (they use usize/isize). |
I think we might want to have tests only run on 32-bit or 64-bit architectures, and have separate paths for that. |
certainly the short-term hack is just to have two tests, one for 32-bit and one for 64-bit? But I thought that the idea of being able to add custom "filters" into the |
Yeah, I hope to get around to adding the two tests later this week, I just need to figure out the specific syntax and what architectures I need to ignore. |
Also moves a few transmute tests to UI tests to better test their output.
7898f41
to
d09cf46
Compare
I just ignored the tests on 32-bit targets. I personally don't know if it's worth testing on those, the tests are just for diagnostics. If we want 32 bit tests I'll spin up a VM to generate those tests. |
ping r? @nikomatsakis Or @rust-lang/compiler I think Niko's out of town, so if someone else can take a look that'd be great! |
@bors r+ |
📌 Commit d09cf46 has been approved by |
@bors r- |
Actually, I have some mild reservations. I just want to be sure: are those tests checking anything important? e.g., do we have other tests that show that the size of points on 32-bit machines is correctly handled, etc? |
I guess my question is -- are there other tests that test similar conditions and do run on 32-bit? |
Yes, there are tests (the compile-fail tests that this edits, but doesn't move to UI, for one) that test all cases as far as I can tell. If you'd like, we can wait on this (probably closing it for now) until we get some form of ERROR-x86 or something like that so we can properly test across both platforms easily. |
@Mark-Simulacrum ok, cool :) |
@bors r+ |
📌 Commit d09cf46 has been approved by |
@bors rollup- Since it failed CI last time. |
Print the two types in the span label for transmute errors. Fixes #37157. I'm not entirely happy with the changes here but overall it's better in my opinion; we certainly avoid the odd language in that issue, which changes to: ``` error[E0512]: transmute called with differently sized types: <C as TypeConstructor<'a>>::T (size can vary because of <C as TypeConstructor>::T) to <C as TypeConstructor<'b>>::T (size can vary because of <C as TypeConstructor>::T) --> test.rs:8:5 | 8 | ::std::mem::transmute(x) | ^^^^^^^^^^^^^^^^^^^^^ transmuting between <C as TypeConstructor<'a>>::T and <C as TypeConstructor<'b>>::T error: aborting due to previous error(s) ```
☀️ Test successful - status-appveyor, status-travis |
… r=nikomatsakis compilertest (UI test): Support custom normalization. Closes #42434. Adds this header for UI tests: ```rust // normalize-stderr-32bit: "fn() (32 bits)" -> "fn() ($PTR bits)" ``` It will normalize the `stderr` output on 32-bit platforms, by replacing all instances of `fn() (32 bits)` by `fn() ($PTR bits)`. Extends the UI tests in #42304 and #41968 to 32-bit targets. r? @nikomatsakis
… r=nikomatsakis compilertest (UI test): Support custom normalization. Closes #42434. Adds this header for UI tests: ```rust // normalize-stderr-32bit: "fn() (32 bits)" -> "fn() ($PTR bits)" ``` It will normalize the `stderr` output on 32-bit platforms, by replacing all instances of `fn() (32 bits)` by `fn() ($PTR bits)`. Extends the UI tests in #42304 and #41968 to 32-bit targets. r? @nikomatsakis
Fixes #37157. I'm not entirely happy with the changes here but overall it's better in my opinion; we certainly avoid the odd language in that issue, which changes to: