Skip to content
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

Copy generics from functions to Return Position Impl Traits in HIR lowering #101345

Closed
wants to merge 53 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
7bbe7b6
Set up new approach for impl trait in return position
spastorino May 11, 2022
b3cc702
Make RPIT not inherit generics from its function
spastorino May 11, 2022
bae1deb
Call mk_param_from_def for all params
spastorino May 20, 2022
a5f8780
Copy generic parameters from function to impl trait opaque type
spastorino May 17, 2022
c1f8fbd
Test nested impl trait in return position
spastorino May 19, 2022
2cccbc4
Add generic type and const args to OpaqueDef
spastorino May 19, 2022
bb28711
Handle all kind of generic args when converting to ty
spastorino May 20, 2022
f60f609
param_env def_id is the one of the opaque type for RPIT
spastorino May 20, 2022
a71699d
Properly gather explicit predicates of opaque type
spastorino May 20, 2022
a311717
is_impl_trait_defn doesn't have any effect now with RPIT
spastorino May 20, 2022
4642ec9
register_member_constraints is 0 for RPIT
spastorino May 20, 2022
35a98a7
gather_explicit_predicates_of return empty for OpaqueTyOrigin::AsyncFn
spastorino Jun 30, 2022
fee7dc6
Properly type check type parameter predicates for RPIT
spastorino May 20, 2022
df9356d
Properly handle Const generics
spastorino May 21, 2022
79eeffa
Pass Generics to lower impl trait instead of generic params
spastorino May 23, 2022
d102712
Copy generic param bounds and where clauses from function to RPIT
spastorino May 23, 2022
8d26963
Remap on lower_res
spastorino Aug 5, 2022
a08ff68
Lower lifetime bounds/predicates as static if not part of RPIT bounds
spastorino Aug 5, 2022
cce5412
Add APIT test for RPIT v2
spastorino May 24, 2022
3b4ab9d
Add bounds regression test for RPIT v2
spastorino May 24, 2022
be33fa2
Add generic associated type test for RPIT v2
spastorino May 24, 2022
c67ab47
Add where bounds test for RPIT v2
spastorino May 24, 2022
064675d
Lower APIT generics to RPIT
spastorino Jun 8, 2022
50bc776
Copy generic params from outer impl/trait blocks to RPIT
spastorino Aug 10, 2022
8f20d42
Add debug call on rustc_middle generics
spastorino Aug 10, 2022
98d43e5
Supress warn on artificial static lifetimes for RPIT
spastorino Aug 10, 2022
cfa73f8
instrument type param predicates and bounds in generic fns
spastorino Jun 30, 2022
9118087
Add APIT with lifetimes test
spastorino Jun 28, 2022
3049a8d
Add a simple APIT test
spastorino Sep 2, 2022
8b5c082
Add lifetimes bounds test
spastorino Sep 2, 2022
1563a66
Do not assert impl_trait_* is_empty
spastorino Jul 5, 2022
54d5fa4
Add debug information
spastorino Jul 5, 2022
faafdf1
Add APIT anonymous lifetime test
spastorino Aug 10, 2022
fff4d31
Add trait bound anonymous lifetime test
spastorino Aug 16, 2022
f08f19e
Create definitions and remap lifetimes in lower_lifetime_binder
spastorino Aug 18, 2022
697393f
save/restore the node-id table for poly-trait-ref
spastorino Sep 2, 2022
9f8a343
impl/block types are first and then fn types
spastorino Aug 23, 2022
f514607
Remove return_position_impl_trait_v2 feature flag and make it default
spastorino Aug 23, 2022
25bbe2a
Remove reborrow in favor of passing &mut
spastorino Sep 2, 2022
bab1d54
Pass generics info from context down to the inner rpits on async fns
spastorino Aug 25, 2022
a19d07e
Push apit nodes before lowering generics and bounds
spastorino Aug 24, 2022
679293a
Do not create new defs and remaps when lowering Universal inside RPITs
spastorino Aug 31, 2022
be98207
Only create def and remap on lower_lifetime_binder for RPITs and not …
spastorino Sep 1, 2022
f16d7fe
Ignore where-clauses wf checks for RPITs they are kinda bogus
spastorino Sep 1, 2022
7200539
Bless impl-trait-captures test as the output is better now
spastorino Aug 25, 2022
a4356ee
Bless async test
spastorino Sep 2, 2022
0791f8d
Bless bound-normalization-fail test
spastorino Aug 25, 2022
8d53dc7
Avoid unneeded repeated code
spastorino Sep 2, 2022
49d057f
itertools is not needed anymore in rustc_ast_lowering
spastorino Sep 2, 2022
0fadeed
Add debug_asserts back
spastorino Sep 5, 2022
835d7de
call lower_span before adding it to GenericArg::Type
spastorino Sep 5, 2022
971641f
Move itctx creation to outside loop
spastorino Sep 5, 2022
d45f1d3
Extend Rustdocs of lower_lifetime_binder
spastorino Sep 5, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,27 @@ impl Default for Generics {
}
}

impl Generics {
pub fn merge(&self, other: &Generics) -> Generics {
let mut params = Vec::with_capacity(self.params.len() + other.params.len());
params.extend(self.params.iter().cloned());
params.extend(other.params.iter().cloned());

let has_where_token =
self.where_clause.has_where_token || other.where_clause.has_where_token;
let mut predicates = Vec::with_capacity(
self.where_clause.predicates.len() + other.where_clause.predicates.len(),
);
predicates.extend(self.where_clause.predicates.iter().cloned());
predicates.extend(other.where_clause.predicates.iter().cloned());

let where_clause =
WhereClause { has_where_token, predicates, span: self.where_clause.span };

Generics { params, where_clause, span: self.span }
}
}

/// A where-clause in a definition.
#[derive(Clone, Encodable, Decodable, Debug)]
pub struct WhereClause {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_lowering/src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
&sym.qself,
&sym.path,
ParamMode::Optional,
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
&mut ImplTraitContext::Disallowed(ImplTraitPosition::Path),
);
hir::InlineAsmOperand::SymStatic { path, def_id }
} else {
Expand Down
7 changes: 3 additions & 4 deletions compiler/rustc_ast_lowering/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
}

fn lower_local(&mut self, l: &Local) -> &'hir hir::Local<'hir> {
let ty = l
.ty
.as_ref()
.map(|t| self.lower_ty(t, ImplTraitContext::Disallowed(ImplTraitPosition::Variable)));
let ty = l.ty.as_ref().map(|t| {
self.lower_ty(t, &mut ImplTraitContext::Disallowed(ImplTraitPosition::Variable))
});
let init = l.kind.init().map(|init| self.lower_expr(init));
let hir_id = self.lower_node_id(l.id);
let pat = self.lower_pat(&l.pat);
Expand Down
121 changes: 74 additions & 47 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
seg,
ParamMode::Optional,
ParenthesizedGenericArgs::Err,
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
&mut ImplTraitContext::Disallowed(ImplTraitPosition::Path),
));
let args = self.arena.alloc_from_iter(
[&*receiver].into_iter().chain(args.iter()).map(|x| self.lower_expr_mut(x)),
Expand All @@ -89,14 +89,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
}
ExprKind::Cast(ref expr, ref ty) => {
let expr = self.lower_expr(expr);
let ty =
self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::Type));
let ty = self
.lower_ty(ty, &mut ImplTraitContext::Disallowed(ImplTraitPosition::Type));
hir::ExprKind::Cast(expr, ty)
}
ExprKind::Type(ref expr, ref ty) => {
let expr = self.lower_expr(expr);
let ty =
self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::Type));
let ty = self
.lower_ty(ty, &mut ImplTraitContext::Disallowed(ImplTraitPosition::Type));
hir::ExprKind::Type(expr, ty)
}
ExprKind::AddrOf(k, m, ref ohs) => {
Expand Down Expand Up @@ -219,7 +219,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
qself,
path,
ParamMode::Optional,
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
&mut ImplTraitContext::Disallowed(ImplTraitPosition::Path),
);
hir::ExprKind::Path(qpath)
}
Expand Down Expand Up @@ -253,7 +253,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
&se.qself,
&se.path,
ParamMode::Optional,
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
&mut ImplTraitContext::Disallowed(ImplTraitPosition::Path),
)),
self.arena
.alloc_from_iter(se.fields.iter().map(|x| self.lower_expr_field(x))),
Expand Down Expand Up @@ -550,12 +550,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
async_gen_kind: hir::AsyncGeneratorKind,
body: impl FnOnce(&mut Self) -> hir::Expr<'hir>,
) -> hir::ExprKind<'hir> {
let output = match ret_ty {
Some(ty) => hir::FnRetTy::Return(
self.lower_ty(&ty, ImplTraitContext::Disallowed(ImplTraitPosition::AsyncBlock)),
),
None => hir::FnRetTy::DefaultReturn(self.lower_span(span)),
};
let output =
match ret_ty {
Some(ty) => hir::FnRetTy::Return(self.lower_ty(
&ty,
&mut ImplTraitContext::Disallowed(ImplTraitPosition::AsyncBlock),
)),
None => hir::FnRetTy::DefaultReturn(self.lower_span(span)),
};

// Resume argument type. We let the compiler infer this to simplify the lowering. It is
// fully constrained by `future::from_generator`.
Expand Down Expand Up @@ -847,21 +849,34 @@ impl<'hir> LoweringContext<'_, 'hir> {
(body_id, generator_option)
});

let bound_generic_params = self.lower_lifetime_binder(closure_id, generic_params);
// Lower outside new scope to preserve `is_in_loop_condition`.
let fn_decl = self.lower_fn_decl(decl, None, FnDeclKind::Closure, None);

let c = self.arena.alloc(hir::Closure {
binder: binder_clause,
capture_clause,
bound_generic_params,
fn_decl,
body: body_id,
fn_decl_span: self.lower_span(fn_decl_span),
movability: generator_option,
});
self.lower_lifetime_binder(
closure_id,
generic_params,
false,
|lctx, bound_generic_params| {
// Lower outside new scope to preserve `is_in_loop_condition`.
let fn_decl = lctx.lower_fn_decl(
decl,
None,
FnDeclKind::Closure,
&Generics::default(),
None,
None,
);

hir::ExprKind::Closure(c)
let c = lctx.arena.alloc(hir::Closure {
binder: binder_clause,
capture_clause,
bound_generic_params,
fn_decl,
body: body_id,
fn_decl_span: lctx.lower_span(fn_decl_span),
movability: generator_option,
});

hir::ExprKind::Closure(c)
},
)
}

fn generator_movability_for_fn(
Expand Down Expand Up @@ -948,23 +963,35 @@ impl<'hir> LoweringContext<'_, 'hir> {
body_id
});

let bound_generic_params = self.lower_lifetime_binder(closure_id, generic_params);

// We need to lower the declaration outside the new scope, because we
// have to conserve the state of being inside a loop condition for the
// closure argument types.
let fn_decl = self.lower_fn_decl(&outer_decl, None, FnDeclKind::Closure, None);

let c = self.arena.alloc(hir::Closure {
binder: binder_clause,
capture_clause,
bound_generic_params,
fn_decl,
body,
fn_decl_span: self.lower_span(fn_decl_span),
movability: None,
});
hir::ExprKind::Closure(c)
self.lower_lifetime_binder(
closure_id,
generic_params,
false,
|lctx, bound_generic_params| {
// We need to lower the declaration outside the new scope, because we
// have to conserve the state of being inside a loop condition for the
// closure argument types.
let fn_decl = lctx.lower_fn_decl(
&outer_decl,
None,
FnDeclKind::Closure,
&Generics::default(),
None,
None,
);

let c = lctx.arena.alloc(hir::Closure {
binder: binder_clause,
capture_clause,
bound_generic_params,
fn_decl,
body,
fn_decl_span: lctx.lower_span(fn_decl_span),
movability: None,
});
hir::ExprKind::Closure(c)
},
)
}

/// Destructure the LHS of complex assignments.
Expand Down Expand Up @@ -1123,7 +1150,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
qself,
path,
ParamMode::Optional,
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
&mut ImplTraitContext::Disallowed(ImplTraitPosition::Path),
);
// Destructure like a tuple struct.
let tuple_struct_pat =
Expand All @@ -1139,7 +1166,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
qself,
path,
ParamMode::Optional,
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
&mut ImplTraitContext::Disallowed(ImplTraitPosition::Path),
);
// Destructure like a unit struct.
let unit_struct_pat = hir::PatKind::Path(qpath);
Expand All @@ -1163,7 +1190,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
&se.qself,
&se.path,
ParamMode::Optional,
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
&mut ImplTraitContext::Disallowed(ImplTraitPosition::Path),
);
let fields_omitted = match &se.rest {
StructRest::Base(e) => {
Expand Down
Loading