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 cd74991

Browse files
committedOct 22, 2024
got defid embedded into contract.
1 parent 1504e07 commit cd74991

File tree

22 files changed

+216
-111
lines changed

22 files changed

+216
-111
lines changed
 

‎compiler/rustc_ast_lowering/src/item.rs

+74-43
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ use rustc_ast::visit::AssocCtxt;
33
use rustc_ast::*;
44
use rustc_errors::ErrorGuaranteed;
55
use rustc_hir as hir;
6-
use rustc_hir::PredicateOrigin;
76
use rustc_hir::def::{DefKind, Res};
8-
use rustc_hir::def_id::{CRATE_DEF_ID, LocalDefId};
7+
use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID};
8+
use rustc_hir::PredicateOrigin;
99
use rustc_index::{IndexSlice, IndexVec};
1010
use rustc_middle::span_bug;
1111
use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
1212
use rustc_span::edit_distance::find_best_match_for_name;
13-
use rustc_span::symbol::{Ident, kw, sym};
13+
use rustc_span::symbol::{kw, sym, Ident};
1414
use rustc_span::{DesugaringKind, Span, Symbol};
1515
use rustc_target::spec::abi;
16-
use smallvec::{SmallVec, smallvec};
16+
use smallvec::{smallvec, SmallVec};
1717
use thin_vec::ThinVec;
1818
use tracing::instrument;
1919

@@ -244,7 +244,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
244244
header: this.lower_fn_header(*header, hir::Safety::Safe),
245245
span: this.lower_span(*fn_sig_span),
246246
};
247-
let contract_ids = this.lower_contract(contract_params, contract);
247+
let contract_ids = this.lower_contract(id, contract_params, contract);
248248
hir::ItemKind::Fn(sig, generics, Some(contract_ids), body_id)
249249
})
250250
}
@@ -286,12 +286,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
286286
);
287287
this.arena.alloc(this.ty(span, hir::TyKind::Err(guar)))
288288
}
289-
Some(ty) => this.lower_ty(ty, ImplTraitContext::OpaqueTy {
290-
origin: hir::OpaqueTyOrigin::TyAlias {
291-
parent: this.local_def_id(id),
292-
in_assoc_ty: false,
289+
Some(ty) => this.lower_ty(
290+
ty,
291+
ImplTraitContext::OpaqueTy {
292+
origin: hir::OpaqueTyOrigin::TyAlias {
293+
parent: this.local_def_id(id),
294+
in_assoc_ty: false,
295+
},
293296
},
294-
}),
297+
),
295298
},
296299
);
297300
hir::ItemKind::TyAlias(ty, generics)
@@ -983,12 +986,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
983986
hir::ImplItemKind::Type(ty)
984987
}
985988
Some(ty) => {
986-
let ty = this.lower_ty(ty, ImplTraitContext::OpaqueTy {
987-
origin: hir::OpaqueTyOrigin::TyAlias {
988-
parent: this.local_def_id(i.id),
989-
in_assoc_ty: true,
989+
let ty = this.lower_ty(
990+
ty,
991+
ImplTraitContext::OpaqueTy {
992+
origin: hir::OpaqueTyOrigin::TyAlias {
993+
parent: this.local_def_id(i.id),
994+
in_assoc_ty: true,
995+
},
990996
},
991-
});
997+
);
992998
hir::ImplItemKind::Type(ty)
993999
}
9941000
},
@@ -1127,30 +1133,52 @@ impl<'hir> LoweringContext<'_, 'hir> {
11271133

11281134
pub(super) fn lower_const_body(&mut self, span: Span, expr: Option<&Expr>) -> hir::BodyId {
11291135
self.lower_body(|this| {
1130-
(&[], match expr {
1131-
Some(expr) => this.lower_expr_mut(expr),
1132-
None => this.expr_err(span, this.dcx().span_delayed_bug(span, "no block")),
1133-
})
1136+
(
1137+
&[],
1138+
match expr {
1139+
Some(expr) => this.lower_expr_mut(expr),
1140+
None => this.expr_err(span, this.dcx().span_delayed_bug(span, "no block")),
1141+
},
1142+
)
11341143
})
11351144
}
11361145

1137-
fn lower_contract(&mut self, params: &'hir [hir::Param<'hir>], contract: &ast::FnContract) -> &'hir hir::FnContractIds {
1146+
fn lower_contract(
1147+
&mut self,
1148+
parent_node_id: NodeId,
1149+
params: &'hir [hir::Param<'hir>],
1150+
contract: &ast::FnContract,
1151+
) -> &'hir hir::FnContractIds {
11381152
let precond: Option<&P<Expr>> = contract.requires.as_ref();
11391153
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-
})
1154+
let precond: Option<hir::Contract> = precond.map(|e| {
1155+
let contract_node_id = self.next_node_id();
1156+
1157+
let def_id = self.create_def(
1158+
self.local_def_id(parent_node_id),
1159+
contract_node_id,
1160+
kw::Empty,
1161+
DefKind::Contract,
1162+
e.span,
1163+
);
1164+
1165+
let body_id = self.lower_body(|this| (params, this.lower_expr_mut(e)));
1166+
hir::Contract { def_id, body_id }
11451167
});
1146-
let postcond: Option<hir::BodyId> = postcond.map(|e| {
1147-
self.lower_body(|this| {
1148-
(params,
1149-
this.lower_expr_mut(e))
1150-
})
1168+
let postcond: Option<hir::Contract> = postcond.map(|e| {
1169+
let contract_node_id = self.next_node_id();
1170+
let def_id = self.create_def(
1171+
self.local_def_id(parent_node_id),
1172+
contract_node_id,
1173+
kw::Empty,
1174+
DefKind::Contract,
1175+
e.span,
1176+
);
1177+
let body_id = self.lower_body(|this| (params, this.lower_expr_mut(e)));
1178+
hir::Contract { def_id, body_id }
11511179
});
1152-
1153-
self.arena.alloc(hir::FnContractIds { precond, postcond, })
1180+
1181+
self.arena.alloc(hir::FnContractIds { precond, postcond })
11541182
}
11551183

11561184
/// Takes what may be the body of an `async fn` or a `gen fn` and wraps it in an `async {}` or
@@ -1529,10 +1557,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
15291557
for bound in &bound_pred.bounds {
15301558
if !matches!(
15311559
*bound,
1532-
GenericBound::Trait(_, TraitBoundModifiers {
1533-
polarity: BoundPolarity::Maybe(_),
1534-
..
1535-
})
1560+
GenericBound::Trait(
1561+
_,
1562+
TraitBoundModifiers { polarity: BoundPolarity::Maybe(_), .. }
1563+
)
15361564
) {
15371565
continue;
15381566
}
@@ -1633,13 +1661,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
16331661
self.children.push((anon_const_did, hir::MaybeOwner::NonOwner(const_id)));
16341662

16351663
let const_body = self.lower_body(|this| {
1636-
(&[], hir::Expr {
1637-
hir_id: const_expr_id,
1638-
kind: hir::ExprKind::Lit(
1639-
this.arena.alloc(hir::Lit { node: LitKind::Bool(true), span }),
1640-
),
1641-
span,
1642-
})
1664+
(
1665+
&[],
1666+
hir::Expr {
1667+
hir_id: const_expr_id,
1668+
kind: hir::ExprKind::Lit(
1669+
this.arena.alloc(hir::Lit { node: LitKind::Bool(true), span }),
1670+
),
1671+
span,
1672+
},
1673+
)
16431674
});
16441675

16451676
let default_ac = self.arena.alloc(hir::AnonConst {

‎compiler/rustc_hir/src/def.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ pub enum DefKind {
109109
Use,
110110
/// An `extern` block.
111111
ForeignMod,
112+
/// A contract associated with a function definition
113+
Contract,
112114
/// Anonymous constant, e.g. the `1 + 2` in `[u8; 1 + 2]`
113115
AnonConst,
114116
/// An inline constant, e.g. `const { 1 + 2 }`
@@ -173,6 +175,7 @@ impl DefKind {
173175
DefKind::LifetimeParam => "lifetime parameter",
174176
DefKind::Use => "import",
175177
DefKind::ForeignMod => "foreign module",
178+
DefKind::Contract => "contract",
176179
DefKind::AnonConst => "constant expression",
177180
DefKind::InlineConst => "inline constant",
178181
DefKind::Field => "field",
@@ -230,7 +233,8 @@ impl DefKind {
230233
DefKind::Macro(..) => Some(Namespace::MacroNS),
231234

232235
// Not namespaced.
233-
DefKind::AnonConst
236+
DefKind::Contract
237+
| DefKind::AnonConst
234238
| DefKind::InlineConst
235239
| DefKind::Field
236240
| DefKind::LifetimeParam
@@ -282,6 +286,7 @@ impl DefKind {
282286
DefKind::Impl { .. } => DefPathData::Impl,
283287
DefKind::Closure => DefPathData::Closure,
284288
DefKind::SyntheticCoroutineBody => DefPathData::Closure,
289+
DefKind::Contract => DefPathData::Contract,
285290
}
286291
}
287292

@@ -323,6 +328,7 @@ impl DefKind {
323328
| DefKind::TyParam
324329
| DefKind::ConstParam
325330
| DefKind::LifetimeParam
331+
| DefKind::Contract
326332
| DefKind::AnonConst
327333
| DefKind::InlineConst
328334
| DefKind::GlobalAsm

‎compiler/rustc_hir/src/definitions.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,8 @@ pub enum DefPathData {
291291
OpaqueTy,
292292
/// An anonymous struct or union type i.e. `struct { foo: Type }` or `union { bar: Type }`
293293
AnonAdt,
294+
/// A contract attached to a function definition
295+
Contract,
294296
}
295297

296298
impl Definitions {
@@ -415,7 +417,7 @@ impl DefPathData {
415417
TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) => Some(name),
416418

417419
Impl | ForeignMod | CrateRoot | Use | GlobalAsm | Closure | Ctor | AnonConst
418-
| OpaqueTy | AnonAdt => None,
420+
| OpaqueTy | AnonAdt | Contract => None,
419421
}
420422
}
421423

@@ -439,6 +441,7 @@ impl DefPathData {
439441
AnonConst => DefPathDataName::Anon { namespace: sym::constant },
440442
OpaqueTy => DefPathDataName::Anon { namespace: sym::opaque },
441443
AnonAdt => DefPathDataName::Anon { namespace: sym::anon_adt },
444+
Contract => DefPathDataName::Anon { namespace: sym::contract },
442445
}
443446
}
444447
}

‎compiler/rustc_hir/src/hir.rs

+29-16
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@ use rustc_macros::{Decodable, Encodable, HashStable_Generic};
1717
use rustc_span::def_id::LocalDefId;
1818
use rustc_span::hygiene::MacroKind;
1919
use rustc_span::source_map::Spanned;
20-
use rustc_span::symbol::{Ident, Symbol, kw, sym};
21-
use rustc_span::{BytePos, DUMMY_SP, ErrorGuaranteed, Span};
20+
use rustc_span::symbol::{kw, sym, Ident, Symbol};
21+
use rustc_span::{BytePos, ErrorGuaranteed, Span, DUMMY_SP};
2222
use rustc_target::asm::InlineAsmRegOrRegClass;
2323
use rustc_target::spec::abi::Abi;
2424
use smallvec::SmallVec;
2525
use tracing::debug;
2626

27-
use crate::LangItem;
2827
use crate::def::{CtorKind, DefKind, Res};
2928
use crate::def_id::{DefId, LocalDefIdMap};
3029
pub(crate) use crate::hir_id::{HirId, ItemLocalId, ItemLocalMap, OwnerId};
3130
use crate::intravisit::FnKind;
31+
use crate::LangItem;
3232

3333
#[derive(Debug, Copy, Clone, HashStable_Generic)]
3434
pub struct Lifetime {
@@ -127,7 +127,11 @@ impl LifetimeName {
127127

128128
impl fmt::Display for Lifetime {
129129
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
130-
if self.ident.name != kw::Empty { self.ident.name.fmt(f) } else { "'_".fmt(f) }
130+
if self.ident.name != kw::Empty {
131+
self.ident.name.fmt(f)
132+
} else {
133+
"'_".fmt(f)
134+
}
131135
}
132136
}
133137

@@ -1266,7 +1270,11 @@ impl DotDotPos {
12661270
}
12671271

12681272
pub fn as_opt_usize(&self) -> Option<usize> {
1269-
if self.0 == u32::MAX { None } else { Some(self.0 as usize) }
1273+
if self.0 == u32::MAX {
1274+
None
1275+
} else {
1276+
Some(self.0 as usize)
1277+
}
12701278
}
12711279
}
12721280

@@ -1441,10 +1449,16 @@ pub struct BodyId {
14411449
pub hir_id: HirId,
14421450
}
14431451

1452+
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, HashStable_Generic)]
1453+
pub struct Contract {
1454+
pub def_id: LocalDefId,
1455+
pub body_id: BodyId,
1456+
}
1457+
14441458
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, HashStable_Generic)]
14451459
pub struct FnContractIds {
1446-
pub precond: Option<BodyId>,
1447-
pub postcond: Option<BodyId>,
1460+
pub precond: Option<Contract>,
1461+
pub postcond: Option<Contract>,
14481462
}
14491463

14501464
/// The body of a function, closure, or constant value. In the case of
@@ -2602,10 +2616,10 @@ impl<'hir> Ty<'hir> {
26022616
fn visit_ty(&mut self, t: &'v Ty<'v>) {
26032617
if matches!(
26042618
&t.kind,
2605-
TyKind::Path(QPath::Resolved(_, Path {
2606-
res: crate::def::Res::SelfTyAlias { .. },
2607-
..
2608-
},))
2619+
TyKind::Path(QPath::Resolved(
2620+
_,
2621+
Path { res: crate::def::Res::SelfTyAlias { .. }, .. },
2622+
))
26092623
) {
26102624
self.0.push(t.span);
26112625
return;
@@ -2948,11 +2962,10 @@ impl<'hir> InlineAsmOperand<'hir> {
29482962
}
29492963

29502964
pub fn is_clobber(&self) -> bool {
2951-
matches!(self, InlineAsmOperand::Out {
2952-
reg: InlineAsmRegOrRegClass::Reg(_),
2953-
late: _,
2954-
expr: None
2955-
})
2965+
matches!(
2966+
self,
2967+
InlineAsmOperand::Out { reg: InlineAsmRegOrRegClass::Reg(_), late: _, expr: None }
2968+
)
29562969
}
29572970
}
29582971

‎compiler/rustc_hir/src/intravisit.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,11 @@ pub trait Visitor<'v>: Sized {
313313
fn visit_id(&mut self, _hir_id: HirId) -> Self::Result {
314314
Self::Result::output()
315315
}
316-
fn visit_contract_ids(&mut self, fn_contract_ids: &FnContractIds) -> Self::Result {
317-
walk_contract_ids(self, fn_contract_ids)
316+
fn visit_contract(&mut self, contract: &Contract) -> Self::Result {
317+
self.visit_nested_body(contract.body_id)
318+
}
319+
fn visit_fn_contract_ids(&mut self, fn_contract_ids: &FnContractIds) -> Self::Result {
320+
walk_fn_contract_ids(self, fn_contract_ids)
318321
}
319322
fn visit_name(&mut self, _name: Symbol) -> Self::Result {
320323
Self::Result::output()
@@ -490,15 +493,15 @@ pub trait Visitor<'v>: Sized {
490493
}
491494
}
492495

493-
pub fn walk_contract_ids<'v, V: Visitor<'v>>(
496+
pub fn walk_fn_contract_ids<'v, V: Visitor<'v>>(
494497
visitor: &mut V,
495498
fn_contract_ids: &FnContractIds,
496499
) -> V::Result {
497-
if let Some(precond_id) = fn_contract_ids.precond {
498-
try_visit!(visitor.visit_nested_body(precond_id));
500+
if let Some(precond) = &fn_contract_ids.precond {
501+
try_visit!(visitor.visit_contract(precond));
499502
}
500-
if let Some(postcond_id) = fn_contract_ids.postcond {
501-
try_visit!(visitor.visit_nested_body(postcond_id));
503+
if let Some(postcond) = &fn_contract_ids.postcond {
504+
try_visit!(visitor.visit_contract(postcond));
502505
}
503506
V::Result::output()
504507
}
@@ -532,7 +535,7 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) -> V::
532535
ItemKind::Fn(ref sig, ref generics, fn_contract_ids, body_id) => {
533536
try_visit!(visitor.visit_id(item.hir_id()));
534537
if let Some(fn_contract_ids) = fn_contract_ids {
535-
try_visit!(visitor.visit_contract_ids(fn_contract_ids));
538+
try_visit!(visitor.visit_fn_contract_ids(fn_contract_ids));
536539
}
537540
try_visit!(visitor.visit_fn(
538541
FnKind::ItemFn(item.ident, generics, sig.header),

0 commit comments

Comments
 (0)
Please sign in to comment.