Skip to content

[WIP] make ast lowering incremental #127262

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

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions compiler/rustc_ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub mod visit;

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

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

Expand Down
74 changes: 74 additions & 0 deletions compiler/rustc_ast/src/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ use std::{slice, vec};
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};

use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_data_structures::sync::{DynSend, DynSync, Lrc};

/// An owned smart pointer.
///
/// See the [module level documentation][crate::ptr] for details.
Expand Down Expand Up @@ -208,3 +210,75 @@ where
(**self).hash_stable(hcx, hasher);
}
}

#[derive(Debug)]
pub enum AstOwnerRef<'a> {
NonOwner,
Synthetic(rustc_span::def_id::LocalDefId),
Crate(&'a crate::Crate),
Item(&'a crate::Item),
TraitItem(&'a crate::AssocItem),
ImplItem(&'a crate::AssocItem),
ForeignItem(&'a crate::ForeignItem),
}

#[derive(Debug)]
enum AstOwnerPtr {
NonOwner,
Synthetic(rustc_span::def_id::LocalDefId),
Crate,
Item(*const crate::Item),
TraitItem(*const crate::AssocItem),
ImplItem(*const crate::AssocItem),
ForeignItem(*const crate::ForeignItem),
}

/// Derived pointer to a part of the AST. This data structure is strongly inspired from
/// `owning_ref`, but with restricted API to suit its single use.
#[derive(Debug)]
pub struct AstOwner {
/// Pointer to the full crate.
krate: Lrc<crate::Crate>,
/// Pointer to an item in that crate.
ptr: AstOwnerPtr,
}

unsafe impl DynSend for AstOwner {}
unsafe impl DynSync for AstOwner {}

impl AstOwner {
/// Create a new `AstOwner` from the crate and an item derived from it.
pub unsafe fn new(krate: Lrc<crate::Crate>, item: AstOwnerRef<'_>) -> AstOwner {
let ptr = match item {
AstOwnerRef::NonOwner => AstOwnerPtr::NonOwner,
AstOwnerRef::Synthetic(def_id) => AstOwnerPtr::Synthetic(def_id),
AstOwnerRef::Crate(_) => AstOwnerPtr::Crate,
AstOwnerRef::Item(item) => AstOwnerPtr::Item(item as *const _),
AstOwnerRef::TraitItem(item) => AstOwnerPtr::TraitItem(item as *const _),
AstOwnerRef::ImplItem(item) => AstOwnerPtr::ImplItem(item as *const _),
AstOwnerRef::ForeignItem(item) => AstOwnerPtr::ForeignItem(item as *const _),
};

AstOwner { krate, ptr }
}

pub fn new_non_owner(krate: Lrc<crate::Crate>) -> AstOwner {
AstOwner { krate, ptr: AstOwnerPtr::NonOwner }
}

pub fn as_ref(&self) -> AstOwnerRef<'_> {
match self.ptr {
AstOwnerPtr::NonOwner => AstOwnerRef::NonOwner,
AstOwnerPtr::Synthetic(def_id) => AstOwnerRef::Synthetic(def_id),
AstOwnerPtr::Crate => AstOwnerRef::Crate(&*self.krate),
// SAFETY: the item is live as long as `krate` is.
AstOwnerPtr::Item(item) => AstOwnerRef::Item(unsafe { &*item }),
// SAFETY: the item is live as long as `krate` is.
AstOwnerPtr::TraitItem(item) => AstOwnerRef::TraitItem(unsafe { &*item }),
// SAFETY: the item is live as long as `krate` is.
AstOwnerPtr::ImplItem(item) => AstOwnerRef::ImplItem(unsafe { &*item }),
// SAFETY: the item is live as long as `krate` is.
AstOwnerPtr::ForeignItem(item) => AstOwnerRef::ForeignItem(unsafe { &*item }),
}
}
}
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 @@ -21,7 +21,7 @@ use rustc_target::asm;
use std::collections::hash_map::Entry;
use std::fmt::Write;

impl<'a, 'hir> LoweringContext<'a, 'hir> {
impl<'hir> LoweringContext<'hir> {
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
pub(crate) fn lower_inline_asm(
&mut self,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_lowering/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rustc_hir as hir;

use smallvec::SmallVec;

impl<'a, 'hir> LoweringContext<'a, 'hir> {
impl<'hir> LoweringContext<'hir> {
pub(super) fn lower_block(
&mut self,
b: &Block,
Expand Down
9 changes: 6 additions & 3 deletions compiler/rustc_ast_lowering/src/delegation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
pub generics: &'hir hir::Generics<'hir>,
}

impl<'hir> LoweringContext<'_, 'hir> {
impl<'hir> LoweringContext<'hir> {
pub(crate) fn delegation_has_self(&self, item_id: NodeId, path_id: NodeId, span: Span) -> bool {
let sig_id = self.get_delegation_sig_id(item_id, path_id, span);
let Ok(sig_id) = sig_id else {
Expand Down Expand Up @@ -253,12 +253,13 @@
let arg = if let Some(block) = block
&& idx == 0
{
let mut self_resolver = SelfResolver {
// TODO

Check failure on line 256 in compiler/rustc_ast_lowering/src/delegation.rs

View workflow job for this annotation

GitHub Actions / PR - mingw-check-tidy

TODO is used for tasks that should be done before merging a PR; If you want to leave a message in the codebase use FIXME
/* let mut self_resolver = SelfResolver {
resolver: this.resolver,
path_id: delegation.id,
self_param_id: pat_node_id,
};
self_resolver.visit_block(block);
self_resolver.visit_block(block); */
let block = this.lower_block(block, false);
this.mk_expr(hir::ExprKind::Block(block, None), block.span)
} else {
Expand Down Expand Up @@ -342,13 +343,15 @@
}
}

#[allow(unused)] // TODO

Check failure on line 346 in compiler/rustc_ast_lowering/src/delegation.rs

View workflow job for this annotation

GitHub Actions / PR - mingw-check-tidy

TODO is used for tasks that should be done before merging a PR; If you want to leave a message in the codebase use FIXME
struct SelfResolver<'a> {
resolver: &'a mut ResolverAstLowering,
path_id: NodeId,
self_param_id: NodeId,
}

impl<'a> SelfResolver<'a> {
#[allow(unused)]
fn try_replace_id(&mut self, id: NodeId) {
if let Some(res) = self.resolver.partial_res_map.get(&id)
&& let Some(Res::Local(sig_id)) = res.full_res()
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use rustc_span::DUMMY_SP;
use rustc_span::{DesugaringKind, Span};
use thin_vec::{thin_vec, ThinVec};

impl<'hir> LoweringContext<'_, 'hir> {
impl<'hir> LoweringContext<'hir> {
fn lower_exprs(&mut self, exprs: &[AstP<Expr>]) -> &'hir [hir::Expr<'hir>] {
self.arena.alloc_from_iter(exprs.iter().map(|x| self.lower_expr_mut(x)))
}
Expand Down Expand Up @@ -1133,7 +1133,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
whole_span: Span,
) -> hir::ExprKind<'hir> {
// Return early in case of an ordinary assignment.
fn is_ordinary(lower_ctx: &mut LoweringContext<'_, '_>, lhs: &Expr) -> bool {
fn is_ordinary(lower_ctx: &mut LoweringContext<'_>, lhs: &Expr) -> bool {
match &lhs.kind {
ExprKind::Array(..)
| ExprKind::Struct(..)
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_ast_lowering/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use rustc_span::{
};
use std::borrow::Cow;

impl<'hir> LoweringContext<'_, 'hir> {
impl<'hir> LoweringContext<'hir> {
pub(crate) fn lower_format_args(&mut self, sp: Span, fmt: &FormatArgs) -> hir::ExprKind<'hir> {
// Never call the const constructor of `fmt::Arguments` if the
// format_args!() had any arguments _before_ flattening/inlining.
Expand Down Expand Up @@ -233,7 +233,7 @@ enum ArgumentType {
/// <core::fmt::Argument>::new_…(arg)
/// ```
fn make_argument<'hir>(
ctx: &mut LoweringContext<'_, 'hir>,
ctx: &mut LoweringContext<'hir>,
sp: Span,
arg: &'hir hir::Expr<'hir>,
ty: ArgumentType,
Expand Down Expand Up @@ -279,7 +279,7 @@ fn make_argument<'hir>(
/// <core::fmt::rt::Count>::Implied
/// ```
fn make_count<'hir>(
ctx: &mut LoweringContext<'_, 'hir>,
ctx: &mut LoweringContext<'hir>,
sp: Span,
count: &Option<FormatCount>,
argmap: &mut FxIndexMap<(usize, ArgumentType), Option<Span>>,
Expand Down Expand Up @@ -332,7 +332,7 @@ fn make_count<'hir>(
/// )
/// ```
fn make_format_spec<'hir>(
ctx: &mut LoweringContext<'_, 'hir>,
ctx: &mut LoweringContext<'hir>,
sp: Span,
placeholder: &FormatPlaceholder,
argmap: &mut FxIndexMap<(usize, ArgumentType), Option<Span>>,
Expand Down Expand Up @@ -391,7 +391,7 @@ fn make_format_spec<'hir>(
}

fn expand_format_args<'hir>(
ctx: &mut LoweringContext<'_, 'hir>,
ctx: &mut LoweringContext<'hir>,
macsp: Span,
fmt: &FormatArgs,
allow_const: bool,
Expand Down
Loading
Loading