Skip to content

Don't lint lifetime-only transmutes #2759

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

Merged
merged 4 commits into from
May 29, 2018

Conversation

devonhollowood
Copy link
Contributor

Fix #2719

// This is just modifying the lifetime, and is one of the recommended uses
// of transmute
let n = 1u32;
let _ = unsafe { std::mem::transmute::<&'_ u32, &'static u32>(&n) };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want transmute::<Foo<'a>, Foo<'b>> to also not lint?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's a good question. Are there reasonable cases where one of the transmuted lifetimes is not 'static? My suspicion is that the answer is yes but I've never run into one.

@sgrif, since you filed the original issue, do you have an opinion here?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, there's definitely use cases where neither lifetime is 'static. The main use case that I hit is literally Foo<'a> to Foo<'b> where 'b is known to be smaller than 'a, but is also unnameable. If Foo is invariant, this is an important use case.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oli-obk In that case, I think transmute::<Foo<'a>, Foo<'b>> should not lint. Do you want me to add a test explicitly checking this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes please

@devonhollowood devonhollowood force-pushed the transmute-same-ptr-ptr branch from d086e8d to 7d57aca Compare May 23, 2018 00:55
@devonhollowood
Copy link
Contributor Author

devonhollowood commented May 23, 2018

@oli-obk Added the test and rebased onto master, but the build is failing. Is master broken at the moment?

edit: looks like the answer is yes. For reference, this pull request works with nightly-2018-05-20.

@phansch
Copy link
Member

phansch commented May 23, 2018

retriggering travis

@phansch phansch closed this May 23, 2018
@phansch phansch reopened this May 23, 2018
@devonhollowood
Copy link
Contributor Author

@phansch Thanks!

@sgrif
Copy link

sgrif commented May 23, 2018

Do you think it's worth adding a test for transmuting from &'a Foo<'b> to &'a Foo<'static>? All the current tests only change the lifetime of the reference itself, which is unlikely to need transmute

@devonhollowood
Copy link
Contributor Author

@sgrif Oh sorry I had misunderstood the request earlier. That's an excellent idea, and this lint does indeed give a false positive for that currently. I'll poke around with this tonight to see if I can fix that.

@devonhollowood
Copy link
Contributor Author

Whew, this was harder than expected! I think it should be working now. Let me know if there are any other test cases you want me to add.

@@ -363,8 +364,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Transmute {
}
)
} else {
// In this case they differ only in lifetime
if ty_from != ty_to {
if !differ_only_in_lifetime_params(from_ty, to_ty) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some functions on TyCtxt which allow you to erase lifetimes. Maybe calling one of those has a similar effect?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how I missed that... I've replaced differ_only_in_lifetime_params() with a couple calls to TyCtxt::erase_regions(). Good catch!

@devonhollowood devonhollowood force-pushed the transmute-same-ptr-ptr branch from c9b6693 to 394cfed Compare May 29, 2018 05:35
@devonhollowood
Copy link
Contributor Author

Updated and rebased

@oli-obk
Copy link
Contributor

oli-obk commented May 29, 2018

can you rebase again? there was a nightly update in between

@devonhollowood devonhollowood force-pushed the transmute-same-ptr-ptr branch from 394cfed to 9118cd6 Compare May 29, 2018 16:36
@devonhollowood
Copy link
Contributor Author

@oli-obk done

@oli-obk oli-obk merged commit b799c1e into rust-lang:master May 29, 2018
@oli-obk
Copy link
Contributor

oli-obk commented May 29, 2018

👍

@sgrif
Copy link

sgrif commented May 30, 2018

Will hopefully be able to test soon (A bug in Rust nightly prevents Diesel from compiling for the past 2 weeks) ❤️

@devonhollowood
Copy link
Contributor Author

@sgrif Let me know how it goes!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants