Skip to content

Commit

Permalink
tail expression behind terminating scope
Browse files Browse the repository at this point in the history
  • Loading branch information
dingxiangfei2009 committed May 19, 2024
1 parent 6c90ac8 commit 21d1190
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 23 deletions.
54 changes: 35 additions & 19 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1716,24 +1716,28 @@ impl<'hir> LoweringContext<'_, 'hir> {
// `mut iter => { ... }`
let iter_arm = self.arm(iter_pat, loop_expr);

let into_iter_expr = match loop_kind {
let match_expr = match loop_kind {
ForLoopKind::For => {
// `::std::iter::IntoIterator::into_iter(<head>)`
self.expr_call_lang_item_fn(
let into_iter_expr = self.expr_call_lang_item_fn(
head_span,
hir::LangItem::IntoIterIntoIter,
arena_vec![self; head],
)
);

self.arena.alloc(self.expr_match(
for_span,
into_iter_expr,
arena_vec![self; iter_arm],
hir::MatchSource::ForLoopDesugar,
))
}
// ` unsafe { Pin::new_unchecked(&mut into_async_iter(<head>)) }`
// `match into_async_iter(<head>) { ref mut iter => match unsafe { Pin::new_unchecked(iter) } { ... } }`
ForLoopKind::ForAwait => {
// `::core::async_iter::IntoAsyncIterator::into_async_iter(<head>)`
let iter = self.expr_call_lang_item_fn(
head_span,
hir::LangItem::IntoAsyncIterIntoIter,
arena_vec![self; head],
);
let iter = self.expr_mut_addr_of(head_span, iter);
let iter_ident = iter;
let (async_iter_pat, async_iter_pat_id) =
self.pat_ident_binding_mode(head_span, iter_ident, hir::BindingMode::REF_MUT);
let iter = self.expr_ident_mut(head_span, iter_ident, async_iter_pat_id);
// `Pin::new_unchecked(...)`
let iter = self.arena.alloc(self.expr_call_lang_item_fn_mut(
head_span,
Expand All @@ -1742,17 +1746,29 @@ impl<'hir> LoweringContext<'_, 'hir> {
));
// `unsafe { ... }`
let iter = self.arena.alloc(self.expr_unsafe(iter));
iter
let inner_match_expr = self.arena.alloc(self.expr_match(
for_span,
iter,
arena_vec![self; iter_arm],
hir::MatchSource::ForLoopDesugar,
));

// `::core::async_iter::IntoAsyncIterator::into_async_iter(<head>)`
let iter = self.expr_call_lang_item_fn(
head_span,
hir::LangItem::IntoAsyncIterIntoIter,
arena_vec![self; head],
);
let iter_arm = self.arm(async_iter_pat, inner_match_expr);
self.arena.alloc(self.expr_match(
for_span,
iter,
arena_vec![self; iter_arm],
hir::MatchSource::ForLoopDesugar,
))
}
};

let match_expr = self.arena.alloc(self.expr_match(
for_span,
into_iter_expr,
arena_vec![self; iter_arm],
hir::MatchSource::ForLoopDesugar,
));

// This is effectively `{ let _result = ...; _result }`.
// The construct was introduced in #21984 and is necessary to make sure that
// temporaries in the `head` expression are dropped and do not leak to the
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_feature/src/unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,8 @@ declare_features! (
(incomplete, return_type_notation, "1.70.0", Some(109417)),
/// Allows `extern "rust-cold"`.
(unstable, rust_cold_cc, "1.63.0", Some(97544)),
/// Shortern the tail expression lifetime
(unstable, shorter_tail_lifetimes, "1.79.0", Some(123739)),
/// Allows the use of SIMD types in functions declared in `extern` blocks.
(unstable, simd_ffi, "1.0.0", Some(27731)),
/// Allows specialization of implementations (RFC 1210).
Expand Down
10 changes: 8 additions & 2 deletions compiler/rustc_hir_analysis/src/check/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//!
//! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/borrow_check.html

use rustc_ast::visit::walk_list;
use rustc_data_structures::fx::FxHashSet;
use rustc_hir as hir;
use rustc_hir::def_id::DefId;
Expand Down Expand Up @@ -167,7 +166,14 @@ fn resolve_block<'tcx>(visitor: &mut RegionResolutionVisitor<'tcx>, blk: &'tcx h
hir::StmtKind::Expr(..) | hir::StmtKind::Semi(..) => visitor.visit_stmt(statement),
}
}
walk_list!(visitor, visit_expr, &blk.expr);
if let Some(tail_expr) = blk.expr {
if visitor.tcx.features().shorter_tail_lifetimes
&& tail_expr.span.edition().at_least_rust_2024()
{
visitor.terminating_scopes.insert(tail_expr.hir_id.local_id);
}
visitor.visit_expr(tail_expr);
}
}

visitor.cx = prev_cx;
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1660,6 +1660,7 @@ symbols! {
shadow_call_stack,
shl,
shl_assign,
shorter_tail_lifetimes,
should_panic,
shr,
shr_assign,
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/feature-gates/feature-gate-shorter_tail_lifetimes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fn f() -> usize {
let c = std::cell::RefCell::new("..");
c.borrow().len() //~ ERROR: `c` does not live long enough
}

fn main() {
let _ = f();
}
26 changes: 26 additions & 0 deletions tests/ui/feature-gates/feature-gate-shorter_tail_lifetimes.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
error[E0597]: `c` does not live long enough
--> $DIR/feature-gate-shorter_tail_lifetimes.rs:3:5
|
LL | let c = std::cell::RefCell::new("..");
| - binding `c` declared here
LL | c.borrow().len()
| ^---------
| |
| borrowed value does not live long enough
| a temporary with access to the borrow is created here ...
LL | }
| -
| |
| `c` dropped here while still borrowed
| ... and the borrow might be used here, when that temporary is dropped and runs the destructor for type `Ref<'_, &str>`
|
= note: the temporary is part of an expression at the end of a block;
consider forcing this temporary to be dropped sooner, before the block's local variables are dropped
help: for example, you could save the expression's value in a new local variable `x` and then make `x` be the expression at the end of the block
|
LL | let x = c.borrow().len(); x
| +++++++ +++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0597`.
26 changes: 26 additions & 0 deletions tests/ui/lifetimes/shorter-tail-expr-lifetime.edition2021.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
error[E0597]: `c` does not live long enough
--> $DIR/shorter-tail-expr-lifetime.rs:10:5
|
LL | let c = std::cell::RefCell::new("..");
| - binding `c` declared here
LL | c.borrow().len()
| ^---------
| |
| borrowed value does not live long enough
| a temporary with access to the borrow is created here ...
LL | }
| -
| |
| `c` dropped here while still borrowed
| ... and the borrow might be used here, when that temporary is dropped and runs the destructor for type `Ref<'_, &str>`
|
= note: the temporary is part of an expression at the end of a block;
consider forcing this temporary to be dropped sooner, before the block's local variables are dropped
help: for example, you could save the expression's value in a new local variable `x` and then make `x` be the expression at the end of the block
|
LL | let x = c.borrow().len(); x
| +++++++ +++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0597`.
15 changes: 15 additions & 0 deletions tests/ui/lifetimes/shorter-tail-expr-lifetime.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//@ revisions: edition2021 edition2024
//@ [edition2024] compile-flags: -Zunstable-options
//@ [edition2024] edition: 2024
//@ [edition2024] run-pass

#![cfg_attr(edition2024, feature(shorter_tail_lifetimes))]

fn f() -> usize {
let c = std::cell::RefCell::new("..");
c.borrow().len() //[edition2021]~ ERROR: `c` does not live long enough
}

fn main() {
let _ = f();
}
4 changes: 2 additions & 2 deletions tests/ui/nll/issue-52534-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ fn foo(x: &u32) -> &u32 {
fn baz(x: &u32) -> &&u32 {
let x = 22;
&&x
//~^ ERROR cannot return value referencing local variable
//~^ ERROR cannot return value referencing local variable `x`
//~| ERROR cannot return reference to temporary value
}

fn foobazbar<'a>(x: u32, y: &'a u32) -> &'a u32 {
let x = 22;
&x
//~^ ERROR cannot return reference to local variable
//~^ ERROR cannot return reference to local variable `x`
}

fn foobar<'a>(x: &'a u32) -> &'a u32 {
Expand Down

0 comments on commit 21d1190

Please sign in to comment.