Skip to content
Open
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
8 changes: 4 additions & 4 deletions compiler/rustc_ast_lowering/src/block.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use rustc_ast::{Block, BlockCheckMode, Local, LocalKind, Stmt, StmtKind};
use rustc_attr_ir::target::Target;
use rustc_attr_ir::target::{AstTarget, Target};
use rustc_hir as hir;
use rustc_span::sym;
use smallvec::SmallVec;
Expand Down Expand Up @@ -37,7 +37,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
match &s.kind {
StmtKind::Let(local) => {
let hir_id = self.lower_node_id(s.id);
let local = self.lower_local(local);
let local = self.lower_local(local, &s);
self.alias_attrs(hir_id, local.hir_id);
let kind = hir::StmtKind::Let(local);
let span = self.lower_span(s.span);
Expand Down Expand Up @@ -88,7 +88,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
}
}

fn lower_local(&mut self, l: &Local) -> &'hir hir::LetStmt<'hir> {
fn lower_local(&mut self, l: &Local, s: &Stmt) -> &'hir hir::LetStmt<'hir> {
// Let statements are allowed to have impl trait in bindings.
let super_ = l.super_.map(|span| self.lower_span(span));
let ty = l.ty.as_ref().map(|t| {
Expand All @@ -104,7 +104,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
};
let span = self.lower_span(l.span);
let source = hir::LocalSource::Normal;
self.lower_attrs(hir_id, &l.attrs, l.span, Target::Statement);
self.lower_attrs(hir_id, &l.attrs, l.span, Target::Statement, AstTarget::Statement(s));
self.arena.alloc(hir::LetStmt { hir_id, super_, ty, pat, init, els, span, source })
}

Expand Down
47 changes: 33 additions & 14 deletions compiler/rustc_ast_lowering/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::Arc;

use rustc_attr_ir::lang_items::LangItem;
use rustc_attr_ir::target::Target;
use rustc_attr_ir::target::{AstTarget, Target};
use rustc_span::sym;
use thin_vec::thin_vec;

Expand Down Expand Up @@ -62,8 +62,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
postcond_checker,
);

let wrapped_body =
self.wrap_body_with_contract_check(body, contract_check, postcond_checker.span);
let wrapped_body = self.wrap_body_with_contract_check(
body,
contract_check,
postcond_checker.span,
ens,
);
self.expr_block(wrapped_body)
}
(None, Some(ens)) => {
Expand Down Expand Up @@ -92,8 +96,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
let contract_check =
self.lower_contract_check_with_postcond(contract_decls, None, postcond_checker);

let wrapped_body =
self.wrap_body_with_contract_check(body, contract_check, postcond_checker.span);
let wrapped_body = self.wrap_body_with_contract_check(
body,
contract_check,
postcond_checker.span,
ens,
);
self.expr_block(wrapped_body)
}
(Some(req), None) => {
Expand Down Expand Up @@ -238,6 +246,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
body: impl FnOnce(&mut Self) -> rustc_hir::Expr<'hir>,
contract_check: &'hir rustc_hir::Expr<'hir>,
postcond_span: rustc_span::Span,
ast_expr: &rustc_ast::Expr,
) -> &'hir rustc_hir::Block<'hir> {
let check_ident: rustc_span::Ident =
rustc_span::Ident::new(sym::__ensures_checker, postcond_span);
Expand Down Expand Up @@ -266,7 +275,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
let body = self.arena.alloc(body(self));

// Finally, inject an ensures check on the implicit return of the body.
let body = self.inject_ensures_check(body, postcond_span, check_ident, check_hir_id);
let body =
self.inject_ensures_check(body, postcond_span, check_ident, check_hir_id, ast_expr);

// Flatten the body into precond, then postcond, then wrapped body.
let wrapped_body = self.block_all(
Expand All @@ -282,14 +292,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
pub(super) fn checked_return(
&mut self,
opt_expr: Option<&'hir rustc_hir::Expr<'hir>>,
ast_expr: &rustc_ast::Expr,
) -> rustc_hir::ExprKind<'hir> {
let checked_ret =
if let Some((check_span, check_ident, check_hir_id)) = self.contract_ensures {
let expr = opt_expr.unwrap_or_else(|| self.expr_unit(check_span));
Some(self.inject_ensures_check(expr, check_span, check_ident, check_hir_id))
} else {
opt_expr
};
let checked_ret = if let Some((check_span, check_ident, check_hir_id)) =
self.contract_ensures
{
let expr = opt_expr.unwrap_or_else(|| self.expr_unit(check_span));
Some(self.inject_ensures_check(expr, check_span, check_ident, check_hir_id, ast_expr))
} else {
opt_expr
};
rustc_hir::ExprKind::Ret(checked_ret)
}

Expand All @@ -300,6 +312,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
span: rustc_span::Span,
cond_ident: rustc_span::Ident,
cond_hir_id: rustc_hir::HirId,
ast_expr: &rustc_ast::Expr,
) -> &'hir rustc_hir::Expr<'hir> {
// {
// let ret = { body };
Expand Down Expand Up @@ -352,7 +365,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
));

let attrs: rustc_ast::AttrVec = thin_vec![self.unreachable_code_attr(span)];
self.lower_attrs(contract_check.hir_id, &attrs, span, Target::Expression);
self.lower_attrs(
contract_check.hir_id,
&attrs,
span,
Target::Expression,
AstTarget::Expression(ast_expr),
);

let ret_block = self.block_all(span, arena_vec![self; ret_stmt], Some(contract_check));
self.arena.alloc(self.expr_block(self.arena.alloc(ret_block)))
Expand Down
65 changes: 50 additions & 15 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rustc_ast::node_id::NodeMap;
use rustc_ast::visit::{Visitor, walk_expr};
use rustc_ast::*;
use rustc_attr_ir::lang_items::LangItem;
use rustc_attr_ir::target::Target;
use rustc_attr_ir::target::{AstTarget, Target};
use rustc_errors::msg;
use rustc_hir as hir;
use rustc_hir::HirId;
Expand Down Expand Up @@ -231,7 +231,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
let old_attrs =
self.curr_owner.attrs.get(&ex.hir_id.local_id).copied().unwrap_or(&[]);
let new_attrs = self
.lower_attrs_vec(&e.attrs, e.span, ex.hir_id, Target::from_expr(e), None)
.lower_attrs_vec(
&e.attrs,
e.span,
ex.hir_id,
Target::from_expr(e),
AstTarget::Expression(e),
)
.into_iter()
.chain(old_attrs.iter().cloned());
let new_attrs = &*self.arena.alloc_from_iter(new_attrs);
Expand All @@ -255,7 +261,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
}

let expr_hir_id = self.lower_node_id(e.id);
self.lower_attrs(expr_hir_id, &e.attrs, e.span, Target::from_expr(e));
self.lower_attrs(
expr_hir_id,
&e.attrs,
e.span,
Target::from_expr(e),
AstTarget::Expression(e),
);

let kind = match &e.kind {
ExprKind::Array(exprs) => hir::ExprKind::Array(self.lower_exprs(exprs)),
Expand Down Expand Up @@ -480,11 +492,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
ExprKind::Continue(opt_label) => {
hir::ExprKind::Continue(self.lower_jump_destination(e.id, *opt_label))
}
ExprKind::Ret(e) => {
let expr = e.as_ref().map(|x| self.lower_expr(x));
self.checked_return(expr)
ExprKind::Ret(box_e) => {
let expr = box_e.as_ref().map(|x| self.lower_expr(x));
self.checked_return(expr, e)
}
ExprKind::Yeet(sub_expr) => self.lower_expr_yeet(e.span, sub_expr.as_deref()),
ExprKind::Yeet(sub_expr) => self.lower_expr_yeet(e.span, sub_expr.as_deref(), e),
ExprKind::Become(sub_expr) => {
let sub_expr = self.lower_expr(sub_expr);
hir::ExprKind::Become(sub_expr)
Expand Down Expand Up @@ -795,7 +807,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let guard = arm.guard.as_ref().map(|guard| self.lower_expr(&guard.cond));
let hir_id = self.next_id();
let span = self.lower_span(arm.span);
self.lower_attrs(hir_id, &arm.attrs, arm.span, Target::Arm);
self.lower_attrs(hir_id, &arm.attrs, arm.span, Target::Arm, AstTarget::Arm(arm));
let is_never_pattern = pat.is_never_pattern();
// We need to lower the body even if it's unneeded for never pattern in match,
// ensure that we can get HirId for DefId if need (issue #137708).
Expand Down Expand Up @@ -1659,7 +1671,7 @@ impl<'hir> LoweringContext<'_, 'hir> {

fn lower_expr_field(&mut self, f: &ExprField) -> hir::ExprField<'hir> {
let hir_id = self.lower_node_id(f.id);
self.lower_attrs(hir_id, &f.attrs, f.span, Target::ExprField);
self.lower_attrs(hir_id, &f.attrs, f.span, Target::ExprField, AstTarget::ExprField(f));
hir::ExprField {
hir_id,
ident: self.lower_ident(f.ident),
Expand Down Expand Up @@ -1926,7 +1938,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
//
// Also, add the attributes to the outer returned expr node.
let expr = self.expr_drop_temps_mut(for_span, match_expr);
self.lower_attrs(expr.hir_id, &e.attrs, e.span, Target::from_expr(e));
self.lower_attrs(
expr.hir_id,
&e.attrs,
e.span,
Target::from_expr(e),
AstTarget::Expression(e),
);
expr
}

Expand Down Expand Up @@ -1974,7 +1992,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
let val_ident = Ident::with_dummy_span(sym::val);
let (val_pat, val_pat_nid) = self.pat_ident(span, val_ident);
let val_expr = self.expr_ident(span, val_ident, val_pat_nid);
self.lower_attrs(val_expr.hir_id, &attrs, span, Target::Expression);
self.lower_attrs(
val_expr.hir_id,
&attrs,
span,
Target::Expression,
AstTarget::Expression(sub_expr),
);
let continue_pat = self.pat_cf_continue(unstable_span, val_pat);
self.arm(continue_pat, val_expr, try_span)
};
Expand Down Expand Up @@ -2013,10 +2037,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
),
))
} else {
let ret_expr = self.checked_return(Some(from_residual_expr));
let ret_expr = self.checked_return(Some(from_residual_expr), sub_expr);
self.arena.alloc(self.expr(try_span, ret_expr))
};
self.lower_attrs(ret_expr.hir_id, &attrs, span, Target::Expression);
self.lower_attrs(
ret_expr.hir_id,
&attrs,
span,
Target::Expression,
AstTarget::Expression(sub_expr),
);

let break_pat = self.pat_cf_break(try_span, residual_local);
self.arm(break_pat, ret_expr, try_span)
Expand All @@ -2038,7 +2068,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
/// ```
/// But to simplify this, there's a `from_yeet` lang item function which
/// handles the combined `FromResidual::from_residual(Yeet(residual))`.
fn lower_expr_yeet(&mut self, span: Span, sub_expr: Option<&Expr>) -> hir::ExprKind<'hir> {
fn lower_expr_yeet(
&mut self,
span: Span,
sub_expr: Option<&Expr>,
ast_expr: &rustc_ast::Expr,
) -> hir::ExprKind<'hir> {
// The expression (if present) or `()` otherwise.
let (yeeted_span, yeeted_expr) = if let Some(sub_expr) = sub_expr {
(sub_expr.span, self.lower_expr(sub_expr))
Expand Down Expand Up @@ -2066,7 +2101,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
Some(from_yeet_expr),
)
}
TryBlockScope::Function => self.checked_return(Some(from_yeet_expr)),
TryBlockScope::Function => self.checked_return(Some(from_yeet_expr), ast_expr),
}
}

Expand Down
10 changes: 8 additions & 2 deletions compiler/rustc_ast_lowering/src/expr/closure.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rustc_ast::*;
use rustc_attr_ir::find_attr;
use rustc_attr_ir::target::Target;
use rustc_attr_ir::target::{AstTarget, Target};
use rustc_hir as hir;
use rustc_hir::HirId;
use rustc_span::{Span, span_bug};
Expand All @@ -20,7 +20,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
closure: &Closure,
) -> hir::Expr<'hir> {
let expr_hir_id = self.lower_node_id(e.id);
let attrs = self.lower_attrs(expr_hir_id, &e.attrs, e.span, Target::from_expr(e));
let attrs = self.lower_attrs(
expr_hir_id,
&e.attrs,
e.span,
Target::from_expr(e),
AstTarget::Closure(closure),
);

match closure.coroutine_marker {
Some(coroutine_marker) => self.lower_expr_coroutine_closure_with_move_exprs(
Expand Down
29 changes: 21 additions & 8 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use rustc_abi::ExternAbi;
use rustc_ast::visit::AssocCtxt;
use rustc_ast::*;
use rustc_attr_ir::target::Target;
use rustc_attr_ir::target::{AstTarget, Target};
use rustc_attr_ir::{AttributeKind, EiiImplResolution, find_attr};
use rustc_errors::{E0570, ErrorGuaranteed, struct_span_code_err};
use rustc_hir::def::{DefKind, Res};
Expand Down Expand Up @@ -165,7 +165,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
&i.attrs,
i.span,
Target::from_ast_item(i),
Some(i),
AstTarget::from_ast_item(&i.kind),
&extra_hir_attributes,
);

Expand Down Expand Up @@ -614,8 +614,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
pub(super) fn lower_foreign_item(&mut self, i: &ForeignItem) -> &'hir hir::ForeignItem<'hir> {
let owner_id = self.curr_owner.owner_id();
let hir_id: HirId = owner_id.into();
let attrs =
self.lower_attrs(hir_id, &i.attrs, i.span, Target::from_foreign_item_kind(&i.kind));
let attrs = self.lower_attrs(
hir_id,
&i.attrs,
i.span,
Target::from_foreign_item_kind(&i.kind),
AstTarget::from_foreign_item_kind(&i.kind),
);
let (ident, kind) = match &i.kind {
ForeignItemKind::Fn(Fn { sig, ident, generics, define_opaque, .. }) => {
let fdec = &sig.decl;
Expand Down Expand Up @@ -693,7 +698,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
self.dcx().span_fatal(v.span, "unnamed enum variants are not yet implemented");
}
let hir_id = self.lower_node_id(v.id);
self.lower_attrs(hir_id, &v.attrs, v.span, Target::Variant);
self.lower_attrs(hir_id, &v.attrs, v.span, Target::Variant, AstTarget::Variant(v));
hir::Variant {
hir_id,
def_id: self.local_def_id(v.id),
Expand Down Expand Up @@ -780,7 +785,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let ty =
self.lower_ty_alloc(&f.ty, ImplTraitContext::Disallowed(ImplTraitPosition::FieldTy));
let hir_id = self.lower_node_id(f.id);
self.lower_attrs(hir_id, &f.attrs, f.span, Target::Field);
self.lower_attrs(hir_id, &f.attrs, f.span, Target::Field, AstTarget::Field(f));
hir::FieldDef {
span: self.lower_span(f.span),
hir_id,
Expand Down Expand Up @@ -808,6 +813,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
&i.attrs,
i.span,
Target::from_assoc_item_kind(&i.kind, AssocCtxt::Trait),
AstTarget::from_assoc_item_kind(&i.kind),
);

let (ident, generics, kind, has_value) = match &i.kind {
Expand Down Expand Up @@ -1070,6 +1076,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
&i.attrs,
i.span,
Target::from_assoc_item_kind(&i.kind, AssocCtxt::Impl { of_trait: is_in_trait_impl }),
AstTarget::from_assoc_item_kind(&i.kind),
);

let (ident, (generics, kind)) = match &i.kind {
Expand Down Expand Up @@ -1238,7 +1245,7 @@ impl<'hir> LoweringContext<'_, 'hir> {

fn lower_param(&mut self, param: &Param) -> hir::Param<'hir> {
let hir_id = self.lower_node_id(param.id);
self.lower_attrs(hir_id, &param.attrs, param.span, Target::Param);
self.lower_attrs(hir_id, &param.attrs, param.span, Target::Param, AstTarget::Param(param));
hir::Param {
hir_id,
pat: self.lower_pat(&param.pat),
Expand Down Expand Up @@ -1938,7 +1945,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
) -> hir::WherePredicate<'hir> {
let hir_id = self.lower_node_id(pred.id);
let span = self.lower_span(pred.span);
self.lower_attrs(hir_id, &pred.attrs, span, Target::WherePredicate);
self.lower_attrs(
hir_id,
&pred.attrs,
span,
Target::WherePredicate,
AstTarget::WherePredicate(pred),
);
let kind = self.arena.alloc(match &pred.kind {
WherePredicateKind::BoundPredicate(WhereBoundPredicate {
bound_generic_params,
Expand Down
Loading
Loading