Skip to content
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

Rollup of 5 pull requests #71046

Closed
wants to merge 18 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
8 changes: 4 additions & 4 deletions src/librustc_error_codes/error_codes/E0515.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
Cannot return value that references local variable

Local variables, function parameters and temporaries are all dropped before the
end of the function body. So a reference to them cannot be returned.
A reference to a local variable was returned.

Erroneous code example:

Expand All @@ -20,6 +17,9 @@ fn get_dangling_iterator<'a>() -> Iter<'a, i32> {
}
```

Local variables, function parameters and temporaries are all dropped before the
end of the function body. So a reference to them cannot be returned.

Consider returning an owned value instead:

```
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_infer/traits/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Trait Resolution. See the [rustc guide] for more information on how this works.
//! Trait Resolution. See the [rustc-dev-guide] for more information on how this works.
//!
//! [rustc guide]: https://rust-lang.github.io/rustc-guide/traits/resolution.html
//! [rustc-dev-guide]: https://rustc-dev-guide.rust-lang.org/traits/resolution.html

mod engine;
pub mod error_reporting;
Expand Down
59 changes: 33 additions & 26 deletions src/librustc_middle/mir/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ macro_rules! make_mir_visitor {
}

macro_rules! visit_place_fns {
(mut) => (
(mut) => {
fn tcx<'a>(&'a self) -> TyCtxt<'tcx>;

fn super_place(
Expand All @@ -849,20 +849,21 @@ macro_rules! visit_place_fns {
) {
self.visit_place_base(&mut place.local, context, location);

if let Some(new_projection) = self.process_projection(&place.projection) {
if let Some(new_projection) = self.process_projection(&place.projection, location) {
place.projection = self.tcx().intern_place_elems(&new_projection);
}
}

fn process_projection(
&mut self,
projection: &'a [PlaceElem<'tcx>],
location: Location,
) -> Option<Vec<PlaceElem<'tcx>>> {
let mut projection = Cow::Borrowed(projection);

for i in 0..projection.len() {
if let Some(elem) = projection.get(i) {
if let Some(elem) = self.process_projection_elem(elem) {
if let Some(elem) = self.process_projection_elem(elem, location) {
// This converts the borrowed projection into `Cow::Owned(_)` and returns a
// clone of the projection so we can mutate and reintern later.
let vec = projection.to_mut();
Expand All @@ -879,13 +880,30 @@ macro_rules! visit_place_fns {

fn process_projection_elem(
&mut self,
_elem: &PlaceElem<'tcx>,
elem: &PlaceElem<'tcx>,
location: Location,
) -> Option<PlaceElem<'tcx>> {
None
match elem {
PlaceElem::Index(local) => {
let mut new_local = *local;
self.visit_local(
&mut new_local,
PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
location,
);

if new_local == *local { None } else { Some(PlaceElem::Index(new_local)) }
}
PlaceElem::Deref
| PlaceElem::Field(..)
| PlaceElem::ConstantIndex { .. }
| PlaceElem::Subslice { .. }
| PlaceElem::Downcast(..) => None,
}
}
);
};

() => (
() => {
fn visit_projection(
&mut self,
local: Local,
Expand All @@ -907,12 +925,7 @@ macro_rules! visit_place_fns {
self.super_projection_elem(local, proj_base, elem, context, location);
}

fn super_place(
&mut self,
place: &Place<'tcx>,
context: PlaceContext,
location: Location,
) {
fn super_place(&mut self, place: &Place<'tcx>, context: PlaceContext, location: Location) {
let mut context = context;

if !place.projection.is_empty() {
Expand All @@ -925,10 +938,7 @@ macro_rules! visit_place_fns {

self.visit_place_base(&place.local, context, location);

self.visit_projection(place.local,
&place.projection,
context,
location);
self.visit_projection(place.local, &place.projection, context, location);
}

fn super_projection(
Expand Down Expand Up @@ -961,19 +971,16 @@ macro_rules! visit_place_fns {
self.visit_local(
local,
PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy),
location
location,
);
}
ProjectionElem::Deref |
ProjectionElem::Subslice { from: _, to: _, from_end: _ } |
ProjectionElem::ConstantIndex { offset: _,
min_length: _,
from_end: _ } |
ProjectionElem::Downcast(_, _) => {
}
ProjectionElem::Deref
| ProjectionElem::Subslice { from: _, to: _, from_end: _ }
| ProjectionElem::ConstantIndex { offset: _, min_length: _, from_end: _ }
| ProjectionElem::Downcast(_, _) => {}
}
}
);
};
}

make_mir_visitor!(Visitor,);
Expand Down
6 changes: 5 additions & 1 deletion src/librustc_mir/borrow_check/renumber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ impl<'a, 'tcx> MutVisitor<'tcx> for NLLVisitor<'a, 'tcx> {
debug!("visit_ty: ty={:?}", ty);
}

fn process_projection_elem(&mut self, elem: &PlaceElem<'tcx>) -> Option<PlaceElem<'tcx>> {
fn process_projection_elem(
&mut self,
elem: &PlaceElem<'tcx>,
_: Location,
) -> Option<PlaceElem<'tcx>> {
if let PlaceElem::Field(field, ty) = elem {
let new_ty = self.renumber_regions(ty);

Expand Down
7 changes: 0 additions & 7 deletions src/librustc_mir/transform/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,6 @@ impl<'tcx> MutVisitor<'tcx> for RenameLocalVisitor<'tcx> {
*local = self.to;
}
}

fn process_projection_elem(&mut self, elem: &PlaceElem<'tcx>) -> Option<PlaceElem<'tcx>> {
match elem {
PlaceElem::Index(local) if *local == self.from => Some(PlaceElem::Index(self.to)),
_ => None,
}
}
}

struct DerefArgVisitor<'tcx> {
Expand Down
12 changes: 0 additions & 12 deletions src/librustc_mir/transform/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -706,18 +706,6 @@ impl<'a, 'tcx> MutVisitor<'tcx> for Integrator<'a, 'tcx> {
self.super_place(place, context, location)
}

fn process_projection_elem(&mut self, elem: &PlaceElem<'tcx>) -> Option<PlaceElem<'tcx>> {
if let PlaceElem::Index(local) = elem {
let new_local = self.make_integrate_local(*local);

if new_local != *local {
return Some(PlaceElem::Index(new_local));
}
}

None
}

fn visit_basic_block_data(&mut self, block: BasicBlock, data: &mut BasicBlockData<'tcx>) {
self.in_cleanup_block = data.is_cleanup;
self.super_basic_block_data(block, data);
Expand Down
9 changes: 0 additions & 9 deletions src/librustc_mir/transform/promote_consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1036,15 +1036,6 @@ impl<'a, 'tcx> MutVisitor<'tcx> for Promoter<'a, 'tcx> {
*local = self.promote_temp(*local);
}
}

fn process_projection_elem(&mut self, elem: &PlaceElem<'tcx>) -> Option<PlaceElem<'tcx>> {
match elem {
PlaceElem::Index(local) if self.is_temp_kind(*local) => {
Some(PlaceElem::Index(self.promote_temp(*local)))
}
_ => None,
}
}
}

pub fn promote_candidates<'tcx>(
Expand Down
7 changes: 0 additions & 7 deletions src/librustc_mir/transform/simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,11 +417,4 @@ impl<'tcx> MutVisitor<'tcx> for LocalUpdater<'tcx> {
fn visit_local(&mut self, l: &mut Local, _: PlaceContext, _: Location) {
*l = self.map[*l].unwrap();
}

fn process_projection_elem(&mut self, elem: &PlaceElem<'tcx>) -> Option<PlaceElem<'tcx>> {
match elem {
PlaceElem::Index(local) => Some(PlaceElem::Index(self.map[*local].unwrap())),
_ => None,
}
}
}
13 changes: 1 addition & 12 deletions src/librustc_mir/util/def_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

use rustc_index::vec::IndexVec;
use rustc_middle::mir::visit::{MutVisitor, PlaceContext, Visitor};
use rustc_middle::mir::{
Body, BodyAndCache, Local, Location, PlaceElem, ReadOnlyBodyAndCache, VarDebugInfo,
};
use rustc_middle::mir::{Body, BodyAndCache, Local, Location, ReadOnlyBodyAndCache, VarDebugInfo};
use rustc_middle::ty::TyCtxt;
use std::mem;

Expand Down Expand Up @@ -157,13 +155,4 @@ impl MutVisitor<'tcx> for MutateUseVisitor<'tcx> {
*local = self.new_local;
}
}

fn process_projection_elem(&mut self, elem: &PlaceElem<'tcx>) -> Option<PlaceElem<'tcx>> {
match elem {
PlaceElem::Index(local) if *local == self.query => {
Some(PlaceElem::Index(self.new_local))
}
_ => None,
}
}
}
6 changes: 4 additions & 2 deletions src/librustc_trait_selection/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
//!
//! - **Traits.** Trait resolution is implemented in the `traits` module.
//!
//! For more information about how rustc works, see the [rustc guide].
//! For more information about how rustc works, see the [rustc-dev-guide].
//!
//! [rustc guide]: https://rust-lang.github.io/rustc-guide/
//! [rustc-dev-guide]: https://rustc-dev-guide.rust-lang.org/
//!
//! # Note
//!
Expand All @@ -16,6 +16,8 @@
#![feature(in_band_lifetimes)]
#![feature(crate_visibility_modifier)]
#![feature(or_patterns)]
#![feature(str_strip)]
#![feature(option_zip)]
#![recursion_limit = "512"] // For rustdoc

#[macro_use]
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_trait_selection/traits/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
// which is somewhat confusing.
self.suggest_restricting_param_bound(
&mut err,
&trait_ref,
trait_ref,
obligation.cause.body_id,
);
} else {
Expand Down
Loading