Skip to content

Commit c4a0b70

Browse files
committed
Auto merge of #127262 - fee1-dead-contrib:lower-incr2, r=<try>
[WIP] make ast lowering incremental cc `@cjgillot`
2 parents d68fe4e + f04016b commit c4a0b70

File tree

30 files changed

+756
-633
lines changed

30 files changed

+756
-633
lines changed

compiler/rustc_ast/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ pub mod visit;
4545

4646
pub use self::ast::*;
4747
pub use self::ast_traits::{AstDeref, AstNodeWrapper, HasAttrs, HasNodeId, HasSpan, HasTokens};
48+
pub use ptr::{AstOwner, AstOwnerRef};
4849

4950
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
5051

compiler/rustc_ast/src/ptr.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ use std::{slice, vec};
2424
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
2525

2626
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
27+
use rustc_data_structures::sync::{DynSend, DynSync, Lrc};
28+
2729
/// An owned smart pointer.
2830
///
2931
/// See the [module level documentation][crate::ptr] for details.
@@ -208,3 +210,75 @@ where
208210
(**self).hash_stable(hcx, hasher);
209211
}
210212
}
213+
214+
#[derive(Debug)]
215+
pub enum AstOwnerRef<'a> {
216+
NonOwner,
217+
Synthetic(rustc_span::def_id::LocalDefId),
218+
Crate(&'a crate::Crate),
219+
Item(&'a crate::Item),
220+
TraitItem(&'a crate::AssocItem),
221+
ImplItem(&'a crate::AssocItem),
222+
ForeignItem(&'a crate::ForeignItem),
223+
}
224+
225+
#[derive(Debug)]
226+
enum AstOwnerPtr {
227+
NonOwner,
228+
Synthetic(rustc_span::def_id::LocalDefId),
229+
Crate,
230+
Item(*const crate::Item),
231+
TraitItem(*const crate::AssocItem),
232+
ImplItem(*const crate::AssocItem),
233+
ForeignItem(*const crate::ForeignItem),
234+
}
235+
236+
/// Derived pointer to a part of the AST. This data structure is strongly inspired from
237+
/// `owning_ref`, but with restricted API to suit its single use.
238+
#[derive(Debug)]
239+
pub struct AstOwner {
240+
/// Pointer to the full crate.
241+
krate: Lrc<crate::Crate>,
242+
/// Pointer to an item in that crate.
243+
ptr: AstOwnerPtr,
244+
}
245+
246+
unsafe impl DynSend for AstOwner {}
247+
unsafe impl DynSync for AstOwner {}
248+
249+
impl AstOwner {
250+
/// Create a new `AstOwner` from the crate and an item derived from it.
251+
pub unsafe fn new(krate: Lrc<crate::Crate>, item: AstOwnerRef<'_>) -> AstOwner {
252+
let ptr = match item {
253+
AstOwnerRef::NonOwner => AstOwnerPtr::NonOwner,
254+
AstOwnerRef::Synthetic(def_id) => AstOwnerPtr::Synthetic(def_id),
255+
AstOwnerRef::Crate(_) => AstOwnerPtr::Crate,
256+
AstOwnerRef::Item(item) => AstOwnerPtr::Item(item as *const _),
257+
AstOwnerRef::TraitItem(item) => AstOwnerPtr::TraitItem(item as *const _),
258+
AstOwnerRef::ImplItem(item) => AstOwnerPtr::ImplItem(item as *const _),
259+
AstOwnerRef::ForeignItem(item) => AstOwnerPtr::ForeignItem(item as *const _),
260+
};
261+
262+
AstOwner { krate, ptr }
263+
}
264+
265+
pub fn new_non_owner(krate: Lrc<crate::Crate>) -> AstOwner {
266+
AstOwner { krate, ptr: AstOwnerPtr::NonOwner }
267+
}
268+
269+
pub fn as_ref(&self) -> AstOwnerRef<'_> {
270+
match self.ptr {
271+
AstOwnerPtr::NonOwner => AstOwnerRef::NonOwner,
272+
AstOwnerPtr::Synthetic(def_id) => AstOwnerRef::Synthetic(def_id),
273+
AstOwnerPtr::Crate => AstOwnerRef::Crate(&*self.krate),
274+
// SAFETY: the item is live as long as `krate` is.
275+
AstOwnerPtr::Item(item) => AstOwnerRef::Item(unsafe { &*item }),
276+
// SAFETY: the item is live as long as `krate` is.
277+
AstOwnerPtr::TraitItem(item) => AstOwnerRef::TraitItem(unsafe { &*item }),
278+
// SAFETY: the item is live as long as `krate` is.
279+
AstOwnerPtr::ImplItem(item) => AstOwnerRef::ImplItem(unsafe { &*item }),
280+
// SAFETY: the item is live as long as `krate` is.
281+
AstOwnerPtr::ForeignItem(item) => AstOwnerRef::ForeignItem(unsafe { &*item }),
282+
}
283+
}
284+
}

compiler/rustc_ast_lowering/src/asm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_target::asm;
2121
use std::collections::hash_map::Entry;
2222
use std::fmt::Write;
2323

24-
impl<'a, 'hir> LoweringContext<'a, 'hir> {
24+
impl<'hir> LoweringContext<'hir> {
2525
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
2626
pub(crate) fn lower_inline_asm(
2727
&mut self,

compiler/rustc_ast_lowering/src/block.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_hir as hir;
44

55
use smallvec::SmallVec;
66

7-
impl<'a, 'hir> LoweringContext<'a, 'hir> {
7+
impl<'hir> LoweringContext<'hir> {
88
pub(super) fn lower_block(
99
&mut self,
1010
b: &Block,

compiler/rustc_ast_lowering/src/delegation.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub(crate) struct DelegationResults<'hir> {
6060
pub generics: &'hir hir::Generics<'hir>,
6161
}
6262

63-
impl<'hir> LoweringContext<'_, 'hir> {
63+
impl<'hir> LoweringContext<'hir> {
6464
pub(crate) fn delegation_has_self(&self, item_id: NodeId, path_id: NodeId, span: Span) -> bool {
6565
let sig_id = self.get_delegation_sig_id(item_id, path_id, span);
6666
let Ok(sig_id) = sig_id else {
@@ -253,12 +253,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
253253
let arg = if let Some(block) = block
254254
&& idx == 0
255255
{
256-
let mut self_resolver = SelfResolver {
256+
// TODO
257+
/* let mut self_resolver = SelfResolver {
257258
resolver: this.resolver,
258259
path_id: delegation.id,
259260
self_param_id: pat_node_id,
260261
};
261-
self_resolver.visit_block(block);
262+
self_resolver.visit_block(block); */
262263
let block = this.lower_block(block, false);
263264
this.mk_expr(hir::ExprKind::Block(block, None), block.span)
264265
} else {
@@ -342,13 +343,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
342343
}
343344
}
344345

346+
#[allow(unused)] // TODO
345347
struct SelfResolver<'a> {
346348
resolver: &'a mut ResolverAstLowering,
347349
path_id: NodeId,
348350
self_param_id: NodeId,
349351
}
350352

351353
impl<'a> SelfResolver<'a> {
354+
#[allow(unused)]
352355
fn try_replace_id(&mut self, id: NodeId) {
353356
if let Some(res) = self.resolver.partial_res_map.get(&id)
354357
&& let Some(Res::Local(sig_id)) = res.full_res()

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use rustc_span::DUMMY_SP;
2424
use rustc_span::{DesugaringKind, Span};
2525
use thin_vec::{thin_vec, ThinVec};
2626

27-
impl<'hir> LoweringContext<'_, 'hir> {
27+
impl<'hir> LoweringContext<'hir> {
2828
fn lower_exprs(&mut self, exprs: &[AstP<Expr>]) -> &'hir [hir::Expr<'hir>] {
2929
self.arena.alloc_from_iter(exprs.iter().map(|x| self.lower_expr_mut(x)))
3030
}
@@ -1133,7 +1133,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11331133
whole_span: Span,
11341134
) -> hir::ExprKind<'hir> {
11351135
// Return early in case of an ordinary assignment.
1136-
fn is_ordinary(lower_ctx: &mut LoweringContext<'_, '_>, lhs: &Expr) -> bool {
1136+
fn is_ordinary(lower_ctx: &mut LoweringContext<'_>, lhs: &Expr) -> bool {
11371137
match &lhs.kind {
11381138
ExprKind::Array(..)
11391139
| ExprKind::Struct(..)

compiler/rustc_ast_lowering/src/format.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_span::{
1212
};
1313
use std::borrow::Cow;
1414

15-
impl<'hir> LoweringContext<'_, 'hir> {
15+
impl<'hir> LoweringContext<'hir> {
1616
pub(crate) fn lower_format_args(&mut self, sp: Span, fmt: &FormatArgs) -> hir::ExprKind<'hir> {
1717
// Never call the const constructor of `fmt::Arguments` if the
1818
// format_args!() had any arguments _before_ flattening/inlining.
@@ -233,7 +233,7 @@ enum ArgumentType {
233233
/// <core::fmt::Argument>::new_…(arg)
234234
/// ```
235235
fn make_argument<'hir>(
236-
ctx: &mut LoweringContext<'_, 'hir>,
236+
ctx: &mut LoweringContext<'hir>,
237237
sp: Span,
238238
arg: &'hir hir::Expr<'hir>,
239239
ty: ArgumentType,
@@ -279,7 +279,7 @@ fn make_argument<'hir>(
279279
/// <core::fmt::rt::Count>::Implied
280280
/// ```
281281
fn make_count<'hir>(
282-
ctx: &mut LoweringContext<'_, 'hir>,
282+
ctx: &mut LoweringContext<'hir>,
283283
sp: Span,
284284
count: &Option<FormatCount>,
285285
argmap: &mut FxIndexMap<(usize, ArgumentType), Option<Span>>,
@@ -332,7 +332,7 @@ fn make_count<'hir>(
332332
/// )
333333
/// ```
334334
fn make_format_spec<'hir>(
335-
ctx: &mut LoweringContext<'_, 'hir>,
335+
ctx: &mut LoweringContext<'hir>,
336336
sp: Span,
337337
placeholder: &FormatPlaceholder,
338338
argmap: &mut FxIndexMap<(usize, ArgumentType), Option<Span>>,
@@ -391,7 +391,7 @@ fn make_format_spec<'hir>(
391391
}
392392

393393
fn expand_format_args<'hir>(
394-
ctx: &mut LoweringContext<'_, 'hir>,
394+
ctx: &mut LoweringContext<'hir>,
395395
macsp: Span,
396396
fmt: &FormatArgs,
397397
allow_const: bool,

0 commit comments

Comments
 (0)