Skip to content

Commit

Permalink
Auto merge of #95573 - cjgillot:lower-query, r=michaelwoerister
Browse files Browse the repository at this point in the history
Make lowering a query

Split from #88186.

This PR refactors the relationship between lowering and the resolver outputs in order to make lowering itself a query.
In a first part, lowering is changed to avoid modifying resolver outputs, by maintaining its own data structures for creating new `NodeId`s and so.

Then, the `TyCtxt` is modified to allow creating new `LocalDefId`s from inside it. This is done by:
- enclosing `Definitions` in a lock, so as to allow modification;
- creating a query `register_def` whose purpose is to declare a `LocalDefId` to the query system.

See `TyCtxt::create_def` and `TyCtxt::iter_local_def_id` for more detailed explanations of the design.
  • Loading branch information
bors committed Jul 7, 2022
2 parents 3e51277 + 32a30ca commit 0f573a0
Show file tree
Hide file tree
Showing 37 changed files with 465 additions and 394 deletions.
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3869,6 +3869,7 @@ name = "rustc_hir"
version = "0.0.0"
dependencies = [
"odht",
"rustc_arena",
"rustc_ast",
"rustc_data_structures",
"rustc_error_messages",
Expand Down
54 changes: 31 additions & 23 deletions compiler/rustc_ast_lowering/src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
) -> &'hir hir::InlineAsm<'hir> {
// Rustdoc needs to support asm! from foreign architectures: don't try
// lowering the register constraints in this case.
let asm_arch = if self.sess.opts.actually_rustdoc { None } else { self.sess.asm_arch };
if asm_arch.is_none() && !self.sess.opts.actually_rustdoc {
struct_span_err!(self.sess, sp, E0472, "inline assembly is unsupported on this target")
.emit();
let asm_arch =
if self.tcx.sess.opts.actually_rustdoc { None } else { self.tcx.sess.asm_arch };
if asm_arch.is_none() && !self.tcx.sess.opts.actually_rustdoc {
struct_span_err!(
self.tcx.sess,
sp,
E0472,
"inline assembly is unsupported on this target"
)
.emit();
}
if let Some(asm_arch) = asm_arch {
// Inline assembly is currently only stable for these architectures.
Expand All @@ -40,9 +46,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
| asm::InlineAsmArch::RiscV32
| asm::InlineAsmArch::RiscV64
);
if !is_stable && !self.sess.features_untracked().asm_experimental_arch {
if !is_stable && !self.tcx.features().asm_experimental_arch {
feature_err(
&self.sess.parse_sess,
&self.tcx.sess.parse_sess,
sym::asm_experimental_arch,
sp,
"inline assembly is not stable yet on this architecture",
Expand All @@ -52,17 +58,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
}
if asm.options.contains(InlineAsmOptions::ATT_SYNTAX)
&& !matches!(asm_arch, Some(asm::InlineAsmArch::X86 | asm::InlineAsmArch::X86_64))
&& !self.sess.opts.actually_rustdoc
&& !self.tcx.sess.opts.actually_rustdoc
{
self.sess
self.tcx
.sess
.struct_span_err(sp, "the `att_syntax` option is only supported on x86")
.emit();
}
if asm.options.contains(InlineAsmOptions::MAY_UNWIND)
&& !self.sess.features_untracked().asm_unwind
{
if asm.options.contains(InlineAsmOptions::MAY_UNWIND) && !self.tcx.features().asm_unwind {
feature_err(
&self.sess.parse_sess,
&self.tcx.sess.parse_sess,
sym::asm_unwind,
sp,
"the `may_unwind` option is unstable",
Expand All @@ -73,20 +78,20 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
let mut clobber_abis = FxHashMap::default();
if let Some(asm_arch) = asm_arch {
for (abi_name, abi_span) in &asm.clobber_abis {
match asm::InlineAsmClobberAbi::parse(asm_arch, &self.sess.target, *abi_name) {
match asm::InlineAsmClobberAbi::parse(asm_arch, &self.tcx.sess.target, *abi_name) {
Ok(abi) => {
// If the abi was already in the list, emit an error
match clobber_abis.get(&abi) {
Some((prev_name, prev_sp)) => {
let mut err = self.sess.struct_span_err(
let mut err = self.tcx.sess.struct_span_err(
*abi_span,
&format!("`{}` ABI specified multiple times", prev_name),
);
err.span_label(*prev_sp, "previously specified here");

// Multiple different abi names may actually be the same ABI
// If the specified ABIs are not the same name, alert the user that they resolve to the same ABI
let source_map = self.sess.source_map();
let source_map = self.tcx.sess.source_map();
if source_map.span_to_snippet(*prev_sp)
!= source_map.span_to_snippet(*abi_span)
{
Expand All @@ -101,16 +106,19 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
}
}
Err(&[]) => {
self.sess
self.tcx
.sess
.struct_span_err(
*abi_span,
"`clobber_abi` is not supported on this target",
)
.emit();
}
Err(supported_abis) => {
let mut err =
self.sess.struct_span_err(*abi_span, "invalid ABI for `clobber_abi`");
let mut err = self
.tcx
.sess
.struct_span_err(*abi_span, "invalid ABI for `clobber_abi`");
let mut abis = format!("`{}`", supported_abis[0]);
for m in &supported_abis[1..] {
let _ = write!(abis, ", `{}`", m);
Expand All @@ -128,7 +136,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
// Lower operands to HIR. We use dummy register classes if an error
// occurs during lowering because we still need to be able to produce a
// valid HIR.
let sess = self.sess;
let sess = self.tcx.sess;
let mut operands: Vec<_> = asm
.operands
.iter()
Expand Down Expand Up @@ -184,9 +192,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
}
}
InlineAsmOperand::Const { ref anon_const } => {
if !self.sess.features_untracked().asm_const {
if !self.tcx.features().asm_const {
feature_err(
&self.sess.parse_sess,
&sess.parse_sess,
sym::asm_const,
*op_sp,
"const operands for inline assembly are unstable",
Expand All @@ -198,9 +206,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
}
}
InlineAsmOperand::Sym { ref sym } => {
if !self.sess.features_untracked().asm_sym {
if !self.tcx.features().asm_sym {
feature_err(
&self.sess.parse_sess,
&sess.parse_sess,
sym::asm_sym,
*op_sp,
"sym operands for inline assembly are unstable",
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_ast_lowering/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
span,
kind: hir::ExprKind::If(let_expr, then_expr, Some(else_expr)),
});
if !self.sess.features_untracked().let_else {
if !self.tcx.features().let_else {
feature_err(
&self.sess.parse_sess,
&self.tcx.sess.parse_sess,
sym::let_else,
local.span,
"`let...else` statements are unstable",
Expand Down
33 changes: 20 additions & 13 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let hir_id = self.lower_node_id(e.id);
return hir::Expr { hir_id, kind, span: self.lower_span(e.span) };
} else {
self.sess
self.tcx.sess
.struct_span_err(
e.span,
"#[rustc_box] requires precisely one argument \
Expand Down Expand Up @@ -207,8 +207,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
self.lower_expr_range(e.span, e1.as_deref(), e2.as_deref(), lims)
}
ExprKind::Underscore => {
self.sess
.struct_span_err(
self.tcx
.sess.struct_span_err(
e.span,
"in expressions, `_` can only be used on the left-hand side of an assignment",
)
Expand Down Expand Up @@ -245,7 +245,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
let rest = match &se.rest {
StructRest::Base(e) => Some(self.lower_expr(e)),
StructRest::Rest(sp) => {
self.sess
self.tcx
.sess
.struct_span_err(*sp, "base expression required after `..`")
.span_label(*sp, "add a base expression here")
.emit();
Expand Down Expand Up @@ -474,7 +475,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
} else {
let try_span = this.mark_span_with_reason(
DesugaringKind::TryBlock,
this.sess.source_map().end_point(body.span),
this.tcx.sess.source_map().end_point(body.span),
this.allow_try_trait.clone(),
);

Expand Down Expand Up @@ -653,7 +654,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
Some(hir::GeneratorKind::Async(_)) => {}
Some(hir::GeneratorKind::Gen) | None => {
let mut err = struct_span_err!(
self.sess,
self.tcx.sess,
dot_await_span,
E0728,
"`await` is only allowed inside `async` functions and blocks"
Expand Down Expand Up @@ -878,7 +879,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
Some(hir::GeneratorKind::Gen) => {
if decl.inputs.len() > 1 {
struct_span_err!(
self.sess,
self.tcx.sess,
fn_decl_span,
E0628,
"too many parameters for a generator (expected 0 or 1 parameters)"
Expand All @@ -892,8 +893,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
}
None => {
if movability == Movability::Static {
struct_span_err!(self.sess, fn_decl_span, E0697, "closures cannot be static")
.emit();
struct_span_err!(
self.tcx.sess,
fn_decl_span,
E0697,
"closures cannot be static"
)
.emit();
}
None
}
Expand All @@ -916,7 +922,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
// FIXME(cramertj): allow `async` non-`move` closures with arguments.
if capture_clause == CaptureBy::Ref && !decl.inputs.is_empty() {
struct_span_err!(
this.sess,
this.tcx.sess,
fn_decl_span,
E0708,
"`async` non-`move` closures with parameters are not currently supported",
Expand Down Expand Up @@ -1163,7 +1169,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
);
let fields_omitted = match &se.rest {
StructRest::Base(e) => {
self.sess
self.tcx
.sess
.struct_span_err(
e.span,
"functional record updates are not allowed in destructuring \
Expand Down Expand Up @@ -1371,7 +1378,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
Some(hir::GeneratorKind::Gen) => {}
Some(hir::GeneratorKind::Async(_)) => {
struct_span_err!(
self.sess,
self.tcx.sess,
span,
E0727,
"`async` generators are not yet supported"
Expand Down Expand Up @@ -1516,7 +1523,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
span,
self.allow_try_trait.clone(),
);
let try_span = self.sess.source_map().end_point(span);
let try_span = self.tcx.sess.source_map().end_point(span);
let try_span = self.mark_span_with_reason(
DesugaringKind::QuestionMark,
try_span,
Expand Down
30 changes: 7 additions & 23 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use super::ResolverAstLoweringExt;
use super::{AstOwner, ImplTraitContext, ImplTraitPosition};
use super::{LoweringContext, ParamMode};
use crate::{Arena, FnDeclKind};
use super::{FnDeclKind, LoweringContext, ParamMode};

use rustc_ast::ptr::P;
use rustc_ast::visit::AssocCtxt;
Expand All @@ -12,12 +11,9 @@ use rustc_errors::struct_span_err;
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID};
use rustc_hir::definitions::Definitions;
use rustc_hir::PredicateOrigin;
use rustc_index::vec::{Idx, IndexVec};
use rustc_middle::ty::{ResolverAstLowering, ResolverOutputs};
use rustc_session::cstore::CrateStoreDyn;
use rustc_session::Session;
use rustc_middle::ty::{DefIdTree, ResolverAstLowering, TyCtxt};
use rustc_span::source_map::DesugaringKind;
use rustc_span::symbol::{kw, sym, Ident};
use rustc_span::Span;
Expand All @@ -27,12 +23,8 @@ use smallvec::{smallvec, SmallVec};
use std::iter;

pub(super) struct ItemLowerer<'a, 'hir> {
pub(super) sess: &'a Session,
pub(super) definitions: &'a mut Definitions,
pub(super) cstore: &'a CrateStoreDyn,
pub(super) resolutions: &'a ResolverOutputs,
pub(super) tcx: TyCtxt<'hir>,
pub(super) resolver: &'a mut ResolverAstLowering,
pub(super) arena: &'hir Arena<'hir>,
pub(super) ast_index: &'a IndexVec<LocalDefId, AstOwner<'a>>,
pub(super) owners: &'a mut IndexVec<LocalDefId, hir::MaybeOwner<&'hir hir::OwnerInfo<'hir>>>,
}
Expand Down Expand Up @@ -65,12 +57,9 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
) {
let mut lctx = LoweringContext {
// Pseudo-globals.
sess: &self.sess,
definitions: self.definitions,
cstore: self.cstore,
resolutions: self.resolutions,
tcx: self.tcx,
resolver: self.resolver,
arena: self.arena,
arena: self.tcx.hir_arena,

// HirId handling.
bodies: Vec::new(),
Expand Down Expand Up @@ -144,12 +133,7 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
fn lower_assoc_item(&mut self, item: &AssocItem, ctxt: AssocCtxt) {
let def_id = self.resolver.node_id_to_def_id[&item.id];

let parent_id = {
let parent = self.definitions.def_key(def_id).parent;
let local_def_index = parent.unwrap();
LocalDefId { local_def_index }
};

let parent_id = self.tcx.local_parent(def_id);
let parent_hir = self.lower_node(parent_id).unwrap();
self.with_lctx(item.id, |lctx| {
// Evaluate with the lifetimes in `params` in-scope.
Expand Down Expand Up @@ -1278,7 +1262,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
}

fn error_on_invalid_abi(&self, abi: StrLit) {
struct_span_err!(self.sess, abi.span, E0703, "invalid ABI: found `{}`", abi.symbol)
struct_span_err!(self.tcx.sess, abi.span, E0703, "invalid ABI: found `{}`", abi.symbol)
.span_label(abi.span, "invalid ABI")
.help(&format!("valid ABIs: {}", abi::all_names().join(", ")))
.emit();
Expand Down
Loading

0 comments on commit 0f573a0

Please sign in to comment.