Skip to content

Commit 3d7e9a7

Browse files
Only record extra lifetime params for async trait fn with no body
1 parent 59c4a92 commit 3d7e9a7

File tree

4 files changed

+44
-88
lines changed

4 files changed

+44
-88
lines changed

compiler/rustc_resolve/src/late.rs

+40-38
Original file line numberDiff line numberDiff line change
@@ -789,8 +789,9 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
789789
let previous_value = self.diagnostic_metadata.current_function;
790790
match fn_kind {
791791
// Bail if the function is foreign, and thus cannot validly have
792-
// a body.
793-
FnKind::Fn(FnCtxt::Foreign, _, sig, _, generics, _) => {
792+
// a body, or if there's no body for some other reason.
793+
FnKind::Fn(FnCtxt::Foreign, _, sig, _, generics, _)
794+
| FnKind::Fn(_, _, sig, _, generics, None) => {
794795
self.visit_fn_header(&sig.header);
795796
self.visit_generics(generics);
796797
self.with_lifetime_rib(
@@ -804,7 +805,12 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
804805
sig.decl.has_self(),
805806
sig.decl.inputs.iter().map(|Param { ty, .. }| (None, &**ty)),
806807
&sig.decl.output,
807-
)
808+
);
809+
810+
this.record_lifetime_params_for_async(
811+
fn_id,
812+
sig.header.asyncness.opt_return_id(),
813+
);
808814
},
809815
);
810816
return;
@@ -846,41 +852,7 @@ impl<'a: 'ast, 'ast> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast> {
846852
},
847853
);
848854

849-
// Construct the list of in-scope lifetime parameters for async lowering.
850-
// We include all lifetime parameters, either named or "Fresh".
851-
// The order of those parameters does not matter, as long as it is
852-
// deterministic.
853-
if let Some((async_node_id, _)) = async_node_id {
854-
let mut extra_lifetime_params = this
855-
.r
856-
.extra_lifetime_params_map
857-
.get(&fn_id)
858-
.cloned()
859-
.unwrap_or_default();
860-
for rib in this.lifetime_ribs.iter().rev() {
861-
extra_lifetime_params.extend(
862-
rib.bindings
863-
.iter()
864-
.map(|(&ident, &(node_id, res))| (ident, node_id, res)),
865-
);
866-
match rib.kind {
867-
LifetimeRibKind::Item => break,
868-
LifetimeRibKind::AnonymousCreateParameter {
869-
binder, ..
870-
} => {
871-
if let Some(earlier_fresh) =
872-
this.r.extra_lifetime_params_map.get(&binder)
873-
{
874-
extra_lifetime_params.extend(earlier_fresh);
875-
}
876-
}
877-
_ => {}
878-
}
879-
}
880-
this.r
881-
.extra_lifetime_params_map
882-
.insert(async_node_id, extra_lifetime_params);
883-
}
855+
this.record_lifetime_params_for_async(fn_id, async_node_id);
884856

885857
if let Some(body) = body {
886858
// Ignore errors in function bodies if this is rustdoc
@@ -3925,6 +3897,36 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
39253897
Some((ident.name, ns)),
39263898
)
39273899
}
3900+
3901+
/// Construct the list of in-scope lifetime parameters for async lowering.
3902+
/// We include all lifetime parameters, either named or "Fresh".
3903+
/// The order of those parameters does not matter, as long as it is
3904+
/// deterministic.
3905+
fn record_lifetime_params_for_async(
3906+
&mut self,
3907+
fn_id: NodeId,
3908+
async_node_id: Option<(NodeId, Span)>,
3909+
) {
3910+
if let Some((async_node_id, _)) = async_node_id {
3911+
let mut extra_lifetime_params =
3912+
self.r.extra_lifetime_params_map.get(&fn_id).cloned().unwrap_or_default();
3913+
for rib in self.lifetime_ribs.iter().rev() {
3914+
extra_lifetime_params.extend(
3915+
rib.bindings.iter().map(|(&ident, &(node_id, res))| (ident, node_id, res)),
3916+
);
3917+
match rib.kind {
3918+
LifetimeRibKind::Item => break,
3919+
LifetimeRibKind::AnonymousCreateParameter { binder, .. } => {
3920+
if let Some(earlier_fresh) = self.r.extra_lifetime_params_map.get(&binder) {
3921+
extra_lifetime_params.extend(earlier_fresh);
3922+
}
3923+
}
3924+
_ => {}
3925+
}
3926+
}
3927+
self.r.extra_lifetime_params_map.insert(async_node_id, extra_lifetime_params);
3928+
}
3929+
}
39283930
}
39293931

39303932
struct LifetimeCountVisitor<'a, 'b> {

src/test/ui/async-await/issue-102138.rs

-46
This file was deleted.

src/test/ui/resolve/issue-69401-trait-fn-no-body-ty-local.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ fn main() {}
22

33
trait Foo {
44
fn fn_with_type_named_same_as_local_in_param(b: b);
5-
//~^ ERROR expected type, found local variable `b`
5+
//~^ ERROR cannot find type `b` in this scope [E0412]
66
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
error[E0573]: expected type, found local variable `b`
1+
error[E0412]: cannot find type `b` in this scope
22
--> $DIR/issue-69401-trait-fn-no-body-ty-local.rs:4:53
33
|
44
LL | fn fn_with_type_named_same_as_local_in_param(b: b);
5-
| ^ not a type
5+
| ^ not found in this scope
66

77
error: aborting due to previous error
88

9-
For more information about this error, try `rustc --explain E0573`.
9+
For more information about this error, try `rustc --explain E0412`.

0 commit comments

Comments
 (0)