Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 80d2036

Browse files
committedOct 16, 2024
work on pretty printing contracts and fixing their lowering.
1 parent 93d3633 commit 80d2036

File tree

4 files changed

+39
-12
lines changed

4 files changed

+39
-12
lines changed
 

‎compiler/rustc_ast_lowering/src/item.rs

+20-7
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
224224
coroutine_kind,
225225
body.as_deref(),
226226
);
227+
let last_recorded_body = this.bodies.last().unwrap();
228+
assert_eq!(last_recorded_body.0, body_id.hir_id.local_id);
229+
let contract_params: &'hir [hir::Param<'hir>] = last_recorded_body.1.params;
227230

228231
let itctx = ImplTraitContext::Universal;
229232
let (generics, decl) =
@@ -241,7 +244,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
241244
header: this.lower_fn_header(*header, hir::Safety::Safe),
242245
span: this.lower_span(*fn_sig_span),
243246
};
244-
let contract_ids = this.lower_contract(contract);
247+
let contract_ids = this.lower_contract(contract_params, contract);
245248
hir::ItemKind::Fn(sig, generics, Some(contract_ids), body_id)
246249
})
247250
}
@@ -1131,13 +1134,23 @@ impl<'hir> LoweringContext<'_, 'hir> {
11311134
})
11321135
}
11331136

1134-
fn lower_contract(&mut self, contract: &ast::FnContract) -> &'hir hir::FnContractIds {
1135-
let precond_expr = contract.requires.as_ref().map(|e| self.lower_expr(&e));
1136-
let postcond_expr = contract.ensures.as_ref().map(|e| self.lower_expr(&e));
1137-
let precond_hir_id: Option<hir::HirId> = precond_expr.map(|e| e.hir_id);
1138-
let postcond_hir_id: Option<hir::HirId> = postcond_expr.map(|e| e.hir_id);
1137+
fn lower_contract(&mut self, params: &'hir [hir::Param<'hir>], contract: &ast::FnContract) -> &'hir hir::FnContractIds {
1138+
let precond: Option<&P<Expr>> = contract.requires.as_ref();
1139+
let postcond: Option<&P<Expr>> = contract.ensures.as_ref();
1140+
let precond: Option<hir::BodyId> = precond.map(|e| {
1141+
self.lower_body(|this| {
1142+
(params,
1143+
this.lower_expr_mut(e))
1144+
})
1145+
});
1146+
let postcond: Option<hir::BodyId> = postcond.map(|e| {
1147+
self.lower_body(|this| {
1148+
(params,
1149+
this.lower_expr_mut(e))
1150+
})
1151+
});
11391152

1140-
self.arena.alloc(hir::FnContractIds { precond_hir_id, postcond_hir_id, })
1153+
self.arena.alloc(hir::FnContractIds { precond, postcond, })
11411154
}
11421155

11431156
/// Takes what may be the body of an `async fn` or a `gen fn` and wraps it in an `async {}` or

‎compiler/rustc_ast_lowering/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
598598
let current_attrs = std::mem::take(&mut self.attrs);
599599
let current_bodies = std::mem::take(&mut self.bodies);
600600
let current_node_ids = std::mem::take(&mut self.node_id_to_local_id);
601-
let current_trait_map = std::mem::take(&mut self.trait_map);
601+
let current_trait_map: rustc_data_structures::unord::UnordMap<rustc_hir::ItemLocalId, Box<[TraitCandidate]>> = std::mem::take(&mut self.trait_map);
602602
let current_owner =
603603
std::mem::replace(&mut self.current_hir_id_owner, hir::OwnerId { def_id });
604604
let current_local_counter =

‎compiler/rustc_hir/src/hir.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1443,8 +1443,8 @@ pub struct BodyId {
14431443

14441444
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, HashStable_Generic)]
14451445
pub struct FnContractIds {
1446-
pub precond_hir_id: Option<HirId>,
1447-
pub postcond_hir_id: Option<HirId>,
1446+
pub precond: Option<BodyId>,
1447+
pub postcond: Option<BodyId>,
14481448
}
14491449

14501450
/// The body of a function, closure, or constant value. In the case of

‎compiler/rustc_hir_pretty/src/lib.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ impl<'a> State<'a> {
363363
generics,
364364
arg_names,
365365
None,
366+
None,
366367
);
367368
self.end(); // end head-ibox
368369
self.word(";");
@@ -522,14 +523,15 @@ impl<'a> State<'a> {
522523
self.word(";");
523524
self.end(); // end the outer cbox
524525
}
525-
hir::ItemKind::Fn(ref sig, generics, _fn_contract_ids, body) => {
526+
hir::ItemKind::Fn(ref sig, generics, fn_contract_ids, body) => {
526527
self.head("");
527528
self.print_fn(
528529
sig.decl,
529530
sig.header,
530531
Some(item.ident.name),
531532
generics,
532533
&[],
534+
fn_contract_ids,
533535
Some(body),
534536
);
535537
self.word(" ");
@@ -792,7 +794,7 @@ impl<'a> State<'a> {
792794
arg_names: &[Ident],
793795
body_id: Option<hir::BodyId>,
794796
) {
795-
self.print_fn(m.decl, m.header, Some(ident.name), generics, arg_names, body_id);
797+
self.print_fn(m.decl, m.header, Some(ident.name), generics, arg_names, None, body_id);
796798
}
797799

798800
fn print_trait_item(&mut self, ti: &hir::TraitItem<'_>) {
@@ -1942,6 +1944,7 @@ impl<'a> State<'a> {
19421944
name: Option<Symbol>,
19431945
generics: &hir::Generics<'_>,
19441946
arg_names: &[Ident],
1947+
fn_contract_ids: Option<&hir::FnContractIds>,
19451948
body_id: Option<hir::BodyId>,
19461949
) {
19471950
self.print_fn_header_info(header);
@@ -1982,6 +1985,7 @@ impl<'a> State<'a> {
19821985
self.pclose();
19831986

19841987
self.print_fn_output(decl);
1988+
if let Some(contract_ids) = fn_contract_ids { self.print_contracts(contract_ids); }
19851989
self.print_where_clause(generics)
19861990
}
19871991

@@ -2146,6 +2150,15 @@ impl<'a> State<'a> {
21462150
self.print_ident(lifetime.ident)
21472151
}
21482152

2153+
fn print_contracts(&mut self, fn_contract_ids: &hir::FnContractIds) {
2154+
if let Some(precond) = fn_contract_ids.precond {
2155+
self.ann.nested(self, Nested::Body(precond));
2156+
}
2157+
if let Some(postcond) = fn_contract_ids.postcond {
2158+
self.ann.nested(self, Nested::Body(postcond));
2159+
}
2160+
}
2161+
21492162
fn print_where_clause(&mut self, generics: &hir::Generics<'_>) {
21502163
if generics.predicates.is_empty() {
21512164
return;
@@ -2260,6 +2273,7 @@ impl<'a> State<'a> {
22602273
generics,
22612274
arg_names,
22622275
None,
2276+
None,
22632277
);
22642278
self.end();
22652279
}

0 commit comments

Comments
 (0)
Please sign in to comment.