Skip to content

Commit 38b7215

Browse files
committedJul 11, 2022
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`
2 parents 9fb32dc + 21a12e8 commit 38b7215

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed
 

‎compiler/rustc_ast_lowering/src/lib.rs

+27
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,33 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11591159
param_mode: ParamMode,
11601160
itctx: ImplTraitContext,
11611161
) -> hir::Ty<'hir> {
1162+
// Check whether we should interpret this as a bare trait object.
1163+
// This check mirrors the one in late resolution. We only introduce this special case in
1164+
// the rare occurence we need to lower `Fresh` anonymous lifetimes.
1165+
// The other cases when a qpath should be opportunistically made a trait object are handled
1166+
// by `ty_path`.
1167+
if qself.is_none()
1168+
&& let Some(partial_res) = self.resolver.get_partial_res(t.id)
1169+
&& partial_res.unresolved_segments() == 0
1170+
&& let Res::Def(DefKind::Trait | DefKind::TraitAlias, _) = partial_res.base_res()
1171+
{
1172+
let (bounds, lifetime_bound) = self.with_dyn_type_scope(true, |this| {
1173+
let bound = this.lower_poly_trait_ref(
1174+
&PolyTraitRef {
1175+
bound_generic_params: vec![],
1176+
trait_ref: TraitRef { path: path.clone(), ref_id: t.id },
1177+
span: t.span
1178+
},
1179+
itctx,
1180+
);
1181+
let bounds = this.arena.alloc_from_iter([bound]);
1182+
let lifetime_bound = this.elided_dyn_bound(t.span);
1183+
(bounds, lifetime_bound)
1184+
});
1185+
let kind = hir::TyKind::TraitObject(bounds, lifetime_bound, TraitObjectSyntax::None);
1186+
return hir::Ty { kind, span: self.lower_span(t.span), hir_id: self.next_id() };
1187+
}
1188+
11621189
let id = self.lower_node_id(t.id);
11631190
let qpath = self.lower_qpath(t.id, qself, path, param_mode, itctx);
11641191
self.ty_path(id, t.span, qpath)

‎compiler/rustc_resolve/src/late.rs

+24
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,30 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
611611
TyKind::Path(ref qself, ref path) => {
612612
self.diagnostic_metadata.current_type_path = Some(ty);
613613
self.smart_resolve_path(ty.id, qself.as_ref(), path, PathSource::Type);
614+
615+
// Check whether we should interpret this as a bare trait object.
616+
if qself.is_none()
617+
&& let Some(partial_res) = self.r.partial_res_map.get(&ty.id)
618+
&& partial_res.unresolved_segments() == 0
619+
&& let Res::Def(DefKind::Trait | DefKind::TraitAlias, _) = partial_res.base_res()
620+
{
621+
// This path is actually a bare trait object. In case of a bare `Fn`-trait
622+
// object with anonymous lifetimes, we need this rib to correctly place the
623+
// synthetic lifetimes.
624+
let span = ty.span.shrink_to_lo().to(path.span.shrink_to_lo());
625+
self.with_generic_param_rib(
626+
&[],
627+
NormalRibKind,
628+
LifetimeRibKind::Generics {
629+
binder: ty.id,
630+
kind: LifetimeBinderKind::PolyTrait,
631+
span,
632+
},
633+
|this| this.visit_path(&path, ty.id),
634+
);
635+
self.diagnostic_metadata.current_type_path = prev_ty;
636+
return;
637+
}
614638
}
615639
TyKind::ImplicitSelf => {
616640
let self_ty = Ident::with_dummy_span(kw::SelfUpper);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#![allow(bare_trait_objects)]
2+
// check-pass
3+
pub struct FormatWith<'a, I, F> {
4+
sep: &'a str,
5+
/// FormatWith uses interior mutability because Display::fmt takes &self.
6+
inner: RefCell<Option<(I, F)>>,
7+
}
8+
9+
use std::cell::RefCell;
10+
use std::fmt;
11+
12+
struct Layout;
13+
14+
pub fn new_format<'a, I, F>(iter: I, separator: &'a str, f: F) -> FormatWith<'a, I, F>
15+
where
16+
I: Iterator,
17+
F: FnMut(I::Item, &mut FnMut(&fmt::Display) -> fmt::Result) -> fmt::Result,
18+
{
19+
FormatWith { sep: separator, inner: RefCell::new(Some((iter, f))) }
20+
}
21+
22+
fn main() {
23+
let _ = new_format(0..32, " | ", |i, f| f(&format_args!("0x{:x}", i)));
24+
}
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Verify that lifetime resolution correctly accounts for `Fn` bare trait objects.
2+
// check-pass
3+
#![allow(bare_trait_objects)]
4+
5+
// This should work as: fn next_u32(fill_buf: &mut dyn FnMut(&mut [u8]))
6+
fn next_u32(fill_buf: &mut FnMut(&mut [u8])) {
7+
let mut buf: [u8; 4] = [0; 4];
8+
fill_buf(&mut buf);
9+
}
10+
11+
fn explicit(fill_buf: &mut dyn FnMut(&mut [u8])) {
12+
let mut buf: [u8; 4] = [0; 4];
13+
fill_buf(&mut buf);
14+
}
15+
16+
fn main() {
17+
let _: fn(&mut FnMut(&mut [u8])) = next_u32;
18+
let _: &dyn Fn(&mut FnMut(&mut [u8])) = &next_u32;
19+
let _: fn(&mut FnMut(&mut [u8])) = explicit;
20+
let _: &dyn Fn(&mut FnMut(&mut [u8])) = &explicit;
21+
let _: fn(&mut dyn FnMut(&mut [u8])) = next_u32;
22+
let _: &dyn Fn(&mut dyn FnMut(&mut [u8])) = &next_u32;
23+
let _: fn(&mut dyn FnMut(&mut [u8])) = explicit;
24+
let _: &dyn Fn(&mut dyn FnMut(&mut [u8])) = &explicit;
25+
}

0 commit comments

Comments
 (0)