2020-07-06 23:49:53 +02:00
|
|
|
//! This module contains the functionality to convert from the wacky tcx data
|
2020-07-21 09:09:27 +00:00
|
|
|
//! structures into the THIR. The `builder` is generally ignorant of the tcx,
|
2019-02-08 14:53:55 +01:00
|
|
|
//! etc., and instead goes through the `Cx` for most of its work.
|
2015-10-05 12:31:48 -04:00
|
|
|
|
2021-04-04 02:24:02 +02:00
|
|
|
use crate::thir::pattern::pat_from_hir;
|
2021-04-04 18:42:17 +02:00
|
|
|
use crate::thir::util::UserAnnotatedTyHelpers;
|
2015-10-05 12:31:48 -04:00
|
|
|
|
2021-04-04 18:42:17 +02:00
|
|
|
use rustc_data_structures::steal::Steal;
|
2022-01-22 18:49:12 -06:00
|
|
|
use rustc_errors::ErrorGuaranteed;
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir as hir;
|
2022-08-22 22:29:25 +02:00
|
|
|
use rustc_hir::def::DefKind;
|
2020-07-06 23:49:53 +02:00
|
|
|
use rustc_hir::def_id::{DefId, LocalDefId};
|
2022-08-27 12:21:02 +02:00
|
|
|
use rustc_hir::lang_items::LangItem;
|
Use larger span for adjustments on method calls
Currently, we use a relatively 'small' span for THIR
expressions generated by an 'adjustment' (e.g. an autoderef,
autoborrow, unsizing). As a result, if a borrow generated
by an adustment ends up causing a borrowcheck error, for example:
```rust
let mut my_var = String::new();
let my_ref = &my_var
my_var.push('a');
my_ref;
```
then the span for the mutable borrow may end up referring
to only the base expression (e.g. `my_var`), rather than
the method call which triggered the mutable borrow
(e.g. `my_var.push('a')`)
Due to a quirk of the MIR borrowck implementation,
this doesn't always get exposed in migration mode,
but it does in many cases.
This commit makes THIR building consistently use 'larger'
spans for adjustment expressions
The intent of this change it make it clearer to users
when it's the specific way in which a variable is
used (for example, in a method call) that produdes
a borrowcheck error. For example, an error message
claiming that a 'mutable borrow occurs here' might
be confusing if it just points at a usage of a variable
(e.g. `my_var`), when no `&mut` is in sight. Pointing
at the entire expression should help to emphasize
that the method call itself is responsible for
the mutable borrow.
In several cases, this makes the `#![feature(nll)]` diagnostic
output match up exactly with the default (migration mode) output.
As a result, several `.nll.stderr` files end up getting removed
entirely.
2021-09-16 15:01:22 -05:00
|
|
|
use rustc_hir::HirId;
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir::Node;
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_middle::middle::region;
|
2021-04-04 02:24:02 +02:00
|
|
|
use rustc_middle::thir::*;
|
2022-09-16 15:31:10 +02:00
|
|
|
use rustc_middle::ty::{self, RvalueScopes, TyCtxt};
|
2021-04-04 02:24:02 +02:00
|
|
|
use rustc_span::Span;
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(crate) fn thir_body<'tcx>(
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2021-03-05 21:56:02 +01:00
|
|
|
owner_def: ty::WithOptConstParam<LocalDefId>,
|
2022-01-22 18:49:12 -06:00
|
|
|
) -> Result<(&'tcx Steal<Thir<'tcx>>, ExprId), ErrorGuaranteed> {
|
2021-04-04 18:42:17 +02:00
|
|
|
let hir = tcx.hir();
|
2022-07-15 23:13:04 -04:00
|
|
|
let body = hir.body(hir.body_owned_by(owner_def.did));
|
2021-04-03 19:58:46 +02:00
|
|
|
let mut cx = Cx::new(tcx, owner_def);
|
2022-01-22 18:49:12 -06:00
|
|
|
if let Some(reported) = cx.typeck_results.tainted_by_errors {
|
|
|
|
return Err(reported);
|
2021-05-22 15:40:26 +02:00
|
|
|
}
|
2021-04-04 18:42:17 +02:00
|
|
|
let expr = cx.mirror_expr(&body.value);
|
2022-08-27 12:21:02 +02:00
|
|
|
|
|
|
|
let owner_id = hir.local_def_id_to_hir_id(owner_def.did);
|
|
|
|
if let Some(ref fn_decl) = hir.fn_decl_by_hir_id(owner_id) {
|
2022-08-22 22:29:25 +02:00
|
|
|
let closure_env_param = cx.closure_env_param(owner_def.did, owner_id);
|
2022-08-27 12:21:02 +02:00
|
|
|
let explicit_params = cx.explicit_params(owner_id, fn_decl, body);
|
2022-08-22 22:29:25 +02:00
|
|
|
cx.thir.params = closure_env_param.into_iter().chain(explicit_params).collect();
|
|
|
|
|
|
|
|
// The resume argument may be missing, in that case we need to provide it here.
|
|
|
|
// It will always be `()` in this case.
|
|
|
|
if tcx.def_kind(owner_def.did) == DefKind::Generator && body.params.is_empty() {
|
|
|
|
cx.thir.params.push(Param {
|
|
|
|
ty: tcx.mk_unit(),
|
|
|
|
pat: None,
|
|
|
|
ty_span: None,
|
|
|
|
self_kind: None,
|
|
|
|
hir_id: None,
|
|
|
|
});
|
|
|
|
}
|
2022-08-27 12:21:02 +02:00
|
|
|
}
|
|
|
|
|
2022-01-22 18:49:12 -06:00
|
|
|
Ok((tcx.alloc_steal_thir(cx.thir), expr))
|
2021-03-05 21:56:02 +01:00
|
|
|
}
|
|
|
|
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(crate) fn thir_tree<'tcx>(
|
2021-07-24 17:18:15 -04:00
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
owner_def: ty::WithOptConstParam<LocalDefId>,
|
|
|
|
) -> String {
|
2022-01-22 18:49:12 -06:00
|
|
|
match thir_body(tcx, owner_def) {
|
|
|
|
Ok((thir, _)) => format!("{:#?}", thir.steal()),
|
|
|
|
Err(_) => "error".into(),
|
|
|
|
}
|
2021-07-24 17:18:15 -04:00
|
|
|
}
|
|
|
|
|
2021-04-03 19:58:46 +02:00
|
|
|
struct Cx<'tcx> {
|
2021-03-05 21:56:02 +01:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2021-04-03 19:58:46 +02:00
|
|
|
thir: Thir<'tcx>,
|
2017-07-26 15:54:44 +03:00
|
|
|
|
2022-08-30 00:10:40 +02:00
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
2017-07-26 15:54:44 +03:00
|
|
|
|
2022-08-30 00:10:40 +02:00
|
|
|
region_scope_tree: &'tcx region::ScopeTree,
|
|
|
|
typeck_results: &'tcx ty::TypeckResults<'tcx>,
|
|
|
|
rvalue_scopes: &'tcx RvalueScopes,
|
2016-05-26 15:42:29 +03:00
|
|
|
|
Use larger span for adjustments on method calls
Currently, we use a relatively 'small' span for THIR
expressions generated by an 'adjustment' (e.g. an autoderef,
autoborrow, unsizing). As a result, if a borrow generated
by an adustment ends up causing a borrowcheck error, for example:
```rust
let mut my_var = String::new();
let my_ref = &my_var
my_var.push('a');
my_ref;
```
then the span for the mutable borrow may end up referring
to only the base expression (e.g. `my_var`), rather than
the method call which triggered the mutable borrow
(e.g. `my_var.push('a')`)
Due to a quirk of the MIR borrowck implementation,
this doesn't always get exposed in migration mode,
but it does in many cases.
This commit makes THIR building consistently use 'larger'
spans for adjustment expressions
The intent of this change it make it clearer to users
when it's the specific way in which a variable is
used (for example, in a method call) that produdes
a borrowcheck error. For example, an error message
claiming that a 'mutable borrow occurs here' might
be confusing if it just points at a usage of a variable
(e.g. `my_var`), when no `&mut` is in sight. Pointing
at the entire expression should help to emphasize
that the method call itself is responsible for
the mutable borrow.
In several cases, this makes the `#![feature(nll)]` diagnostic
output match up exactly with the default (migration mode) output.
As a result, several `.nll.stderr` files end up getting removed
entirely.
2021-09-16 15:01:22 -05:00
|
|
|
/// When applying adjustments to the expression
|
|
|
|
/// with the given `HirId`, use the given `Span`,
|
|
|
|
/// instead of the usual span. This is used to
|
|
|
|
/// assign the span of an overall method call
|
|
|
|
/// (e.g. `my_val.foo()`) to the adjustment expressions
|
|
|
|
/// for the receiver.
|
|
|
|
adjustment_span: Option<(HirId, Span)>,
|
|
|
|
|
2022-08-03 04:30:13 -07:00
|
|
|
/// False to indicate that adjustments should not be applied. Only used for `custom_mir`
|
|
|
|
apply_adjustments: bool,
|
|
|
|
|
2019-05-07 04:40:36 +03:00
|
|
|
/// The `DefId` of the owner of this body.
|
|
|
|
body_owner: DefId,
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
2021-04-03 19:58:46 +02:00
|
|
|
impl<'tcx> Cx<'tcx> {
|
|
|
|
fn new(tcx: TyCtxt<'tcx>, def: ty::WithOptConstParam<LocalDefId>) -> Cx<'tcx> {
|
2020-07-17 08:47:04 +00:00
|
|
|
let typeck_results = tcx.typeck_opt_const_arg(def);
|
2022-08-03 04:30:13 -07:00
|
|
|
let did = def.did;
|
|
|
|
let hir = tcx.hir();
|
2017-07-26 15:54:44 +03:00
|
|
|
Cx {
|
|
|
|
tcx,
|
2021-04-03 19:58:46 +02:00
|
|
|
thir: Thir::new(),
|
2020-07-06 23:49:53 +02:00
|
|
|
param_env: tcx.param_env(def.did),
|
2022-05-25 13:52:32 +08:00
|
|
|
region_scope_tree: tcx.region_scope_tree(def.did),
|
2020-07-17 08:47:04 +00:00
|
|
|
typeck_results,
|
2022-04-01 21:12:18 +08:00
|
|
|
rvalue_scopes: &typeck_results.rvalue_scopes,
|
2022-08-03 04:30:13 -07:00
|
|
|
body_owner: did.to_def_id(),
|
Use larger span for adjustments on method calls
Currently, we use a relatively 'small' span for THIR
expressions generated by an 'adjustment' (e.g. an autoderef,
autoborrow, unsizing). As a result, if a borrow generated
by an adustment ends up causing a borrowcheck error, for example:
```rust
let mut my_var = String::new();
let my_ref = &my_var
my_var.push('a');
my_ref;
```
then the span for the mutable borrow may end up referring
to only the base expression (e.g. `my_var`), rather than
the method call which triggered the mutable borrow
(e.g. `my_var.push('a')`)
Due to a quirk of the MIR borrowck implementation,
this doesn't always get exposed in migration mode,
but it does in many cases.
This commit makes THIR building consistently use 'larger'
spans for adjustment expressions
The intent of this change it make it clearer to users
when it's the specific way in which a variable is
used (for example, in a method call) that produdes
a borrowcheck error. For example, an error message
claiming that a 'mutable borrow occurs here' might
be confusing if it just points at a usage of a variable
(e.g. `my_var`), when no `&mut` is in sight. Pointing
at the entire expression should help to emphasize
that the method call itself is responsible for
the mutable borrow.
In several cases, this makes the `#![feature(nll)]` diagnostic
output match up exactly with the default (migration mode) output.
As a result, several `.nll.stderr` files end up getting removed
entirely.
2021-09-16 15:01:22 -05:00
|
|
|
adjustment_span: None,
|
2022-08-03 04:30:13 -07:00
|
|
|
apply_adjustments: hir
|
|
|
|
.attrs(hir.local_def_id_to_hir_id(did))
|
|
|
|
.iter()
|
|
|
|
.all(|attr| attr.name_or_empty() != rustc_span::sym::custom_mir),
|
2017-07-26 15:54:44 +03:00
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
2022-08-31 13:01:10 +00:00
|
|
|
#[instrument(level = "debug", skip(self))]
|
2022-08-30 00:10:40 +02:00
|
|
|
fn pattern_from_hir(&mut self, p: &hir::Pat<'_>) -> Box<Pat<'tcx>> {
|
2019-09-25 15:36:14 -04:00
|
|
|
let p = match self.tcx.hir().get(p.hir_id) {
|
2022-06-28 13:15:30 -05:00
|
|
|
Node::Pat(p) => p,
|
2017-08-04 00:41:44 +03:00
|
|
|
node => bug!("pattern became {:?}", node),
|
|
|
|
};
|
2021-04-04 02:24:02 +02:00
|
|
|
pat_from_hir(self.tcx, self.param_env, self.typeck_results(), p)
|
2017-08-04 00:41:44 +03:00
|
|
|
}
|
2022-08-27 12:21:02 +02:00
|
|
|
|
2022-08-22 22:29:25 +02:00
|
|
|
fn closure_env_param(&self, owner_def: LocalDefId, owner_id: HirId) -> Option<Param<'tcx>> {
|
|
|
|
match self.tcx.def_kind(owner_def) {
|
|
|
|
DefKind::Closure => {
|
|
|
|
let closure_ty = self.typeck_results.node_type(owner_id);
|
|
|
|
|
|
|
|
let ty::Closure(closure_def_id, closure_substs) = *closure_ty.kind() else {
|
|
|
|
bug!("closure expr does not have closure type: {:?}", closure_ty);
|
|
|
|
};
|
|
|
|
|
|
|
|
let bound_vars = self.tcx.mk_bound_variable_kinds(std::iter::once(
|
|
|
|
ty::BoundVariableKind::Region(ty::BrEnv),
|
|
|
|
));
|
|
|
|
let br = ty::BoundRegion {
|
|
|
|
var: ty::BoundVar::from_usize(bound_vars.len() - 1),
|
|
|
|
kind: ty::BrEnv,
|
|
|
|
};
|
|
|
|
let env_region = ty::ReLateBound(ty::INNERMOST, br);
|
|
|
|
let closure_env_ty =
|
|
|
|
self.tcx.closure_env_ty(closure_def_id, closure_substs, env_region).unwrap();
|
|
|
|
let liberated_closure_env_ty = self.tcx.erase_late_bound_regions(
|
|
|
|
ty::Binder::bind_with_vars(closure_env_ty, bound_vars),
|
|
|
|
);
|
|
|
|
let env_param = Param {
|
|
|
|
ty: liberated_closure_env_ty,
|
|
|
|
pat: None,
|
|
|
|
ty_span: None,
|
|
|
|
self_kind: None,
|
|
|
|
hir_id: None,
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(env_param)
|
|
|
|
}
|
|
|
|
DefKind::Generator => {
|
|
|
|
let gen_ty = self.typeck_results.node_type(owner_id);
|
|
|
|
let gen_param =
|
|
|
|
Param { ty: gen_ty, pat: None, ty_span: None, self_kind: None, hir_id: None };
|
|
|
|
Some(gen_param)
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-27 12:21:02 +02:00
|
|
|
fn explicit_params<'a>(
|
|
|
|
&'a mut self,
|
|
|
|
owner_id: HirId,
|
|
|
|
fn_decl: &'tcx hir::FnDecl<'tcx>,
|
|
|
|
body: &'tcx hir::Body<'tcx>,
|
|
|
|
) -> impl Iterator<Item = Param<'tcx>> + 'a {
|
|
|
|
let fn_sig = self.typeck_results.liberated_fn_sigs()[owner_id];
|
|
|
|
|
|
|
|
body.params.iter().enumerate().map(move |(index, param)| {
|
|
|
|
let ty_span = fn_decl
|
|
|
|
.inputs
|
|
|
|
.get(index)
|
|
|
|
// Make sure that inferred closure args have no type span
|
|
|
|
.and_then(|ty| if param.pat.span != ty.span { Some(ty.span) } else { None });
|
|
|
|
|
|
|
|
let self_kind = if index == 0 && fn_decl.implicit_self.has_implicit_self() {
|
|
|
|
Some(fn_decl.implicit_self)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
// C-variadic fns also have a `VaList` input that's not listed in `fn_sig`
|
|
|
|
// (as it's created inside the body itself, not passed in from outside).
|
|
|
|
let ty = if fn_decl.c_variadic && index == fn_decl.inputs.len() {
|
|
|
|
let va_list_did = self.tcx.require_lang_item(LangItem::VaList, Some(param.span));
|
|
|
|
|
|
|
|
self.tcx
|
|
|
|
.bound_type_of(va_list_did)
|
|
|
|
.subst(self.tcx, &[self.tcx.lifetimes.re_erased.into()])
|
|
|
|
} else {
|
|
|
|
fn_sig.inputs()[index]
|
|
|
|
};
|
|
|
|
|
|
|
|
let pat = self.pattern_from_hir(param.pat);
|
2022-08-22 22:29:25 +02:00
|
|
|
Param { pat: Some(pat), ty, ty_span, self_kind, hir_id: Some(param.hir_id) }
|
2022-08-27 12:21:02 +02:00
|
|
|
})
|
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
2021-04-03 19:58:46 +02:00
|
|
|
impl<'tcx> UserAnnotatedTyHelpers<'tcx> for Cx<'tcx> {
|
2019-06-14 00:48:52 +03:00
|
|
|
fn tcx(&self) -> TyCtxt<'tcx> {
|
2021-03-03 16:35:54 +01:00
|
|
|
self.tcx
|
2018-10-02 11:00:57 -04:00
|
|
|
}
|
|
|
|
|
2020-07-17 08:47:04 +00:00
|
|
|
fn typeck_results(&self) -> &ty::TypeckResults<'tcx> {
|
2021-03-03 16:35:54 +01:00
|
|
|
self.typeck_results
|
2018-10-02 11:00:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-18 17:59:21 -04:00
|
|
|
mod block;
|
|
|
|
mod expr;
|