-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #98637 - cjgillot:bare-trait-anon-lt, r=petrochenkov
Create fresh lifetime parameters for bare fn trait too The current code fails to account for the equivalence between `dyn FnMut(&mut u8)` and bare `FnMut(&mut u8)`, and treated them differently. This PR introduces a special case for `Fn` traits, which are always fully resolved. Fixes #98616 Fixes #98726 This will require a beta-backport, as beta contains that bug. r? `@petrochenkov`
- Loading branch information
Showing
4 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#![allow(bare_trait_objects)] | ||
// check-pass | ||
pub struct FormatWith<'a, I, F> { | ||
sep: &'a str, | ||
/// FormatWith uses interior mutability because Display::fmt takes &self. | ||
inner: RefCell<Option<(I, F)>>, | ||
} | ||
|
||
use std::cell::RefCell; | ||
use std::fmt; | ||
|
||
struct Layout; | ||
|
||
pub fn new_format<'a, I, F>(iter: I, separator: &'a str, f: F) -> FormatWith<'a, I, F> | ||
where | ||
I: Iterator, | ||
F: FnMut(I::Item, &mut FnMut(&fmt::Display) -> fmt::Result) -> fmt::Result, | ||
{ | ||
FormatWith { sep: separator, inner: RefCell::new(Some((iter, f))) } | ||
} | ||
|
||
fn main() { | ||
let _ = new_format(0..32, " | ", |i, f| f(&format_args!("0x{:x}", i))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Verify that lifetime resolution correctly accounts for `Fn` bare trait objects. | ||
// check-pass | ||
#![allow(bare_trait_objects)] | ||
|
||
// This should work as: fn next_u32(fill_buf: &mut dyn FnMut(&mut [u8])) | ||
fn next_u32(fill_buf: &mut FnMut(&mut [u8])) { | ||
let mut buf: [u8; 4] = [0; 4]; | ||
fill_buf(&mut buf); | ||
} | ||
|
||
fn explicit(fill_buf: &mut dyn FnMut(&mut [u8])) { | ||
let mut buf: [u8; 4] = [0; 4]; | ||
fill_buf(&mut buf); | ||
} | ||
|
||
fn main() { | ||
let _: fn(&mut FnMut(&mut [u8])) = next_u32; | ||
let _: &dyn Fn(&mut FnMut(&mut [u8])) = &next_u32; | ||
let _: fn(&mut FnMut(&mut [u8])) = explicit; | ||
let _: &dyn Fn(&mut FnMut(&mut [u8])) = &explicit; | ||
let _: fn(&mut dyn FnMut(&mut [u8])) = next_u32; | ||
let _: &dyn Fn(&mut dyn FnMut(&mut [u8])) = &next_u32; | ||
let _: fn(&mut dyn FnMut(&mut [u8])) = explicit; | ||
let _: &dyn Fn(&mut dyn FnMut(&mut [u8])) = &explicit; | ||
} |