Skip to content

Commit 4ac434e

Browse files
committed
Auto merge of #60434 - Centril:rollup-x1iczrh, r=Centril
Rollup of 7 pull requests Successful merges: - #60327 (Search for incompatible universes in borrow errors) - #60330 (Suggest using an inclusive range instead of an exclusive range when the endpoint overflows by 1) - #60366 (build-gcc: Create missing cc symlink) - #60369 (Support ZSTs in DispatchFromDyn) - #60404 (Implement `BorrowMut<str>` for `String`) - #60417 (Rename hir::ExprKind::Use to ::DropTemps and improve docs.) - #60432 (Update clippy) Failed merges: r? @ghost
2 parents 834bd19 + 9fe04ef commit 4ac434e

40 files changed

+731
-437
lines changed

src/ci/docker/dist-x86_64-linux/build-gcc.sh

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ hide_output ../gcc-$GCC/configure \
3232
--enable-languages=c,c++
3333
hide_output make -j10
3434
hide_output make install
35+
ln -s gcc /rustroot/bin/cc
3536

3637
cd ..
3738
rm -rf gcc-build

src/liballoc/str.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
// It's cleaner to just turn off the unused_imports warning than to fix them.
2929
#![allow(unused_imports)]
3030

31-
use core::borrow::Borrow;
31+
use core::borrow::{Borrow, BorrowMut};
3232
use core::str::pattern::{Pattern, Searcher, ReverseSearcher, DoubleEndedSearcher};
3333
use core::mem;
3434
use core::ptr;
@@ -190,6 +190,14 @@ impl Borrow<str> for String {
190190
}
191191
}
192192

193+
#[stable(feature = "string_borrow_mut", since = "1.36.0")]
194+
impl BorrowMut<str> for String {
195+
#[inline]
196+
fn borrow_mut(&mut self) -> &mut str {
197+
&mut self[..]
198+
}
199+
}
200+
193201
#[stable(feature = "rust1", since = "1.0.0")]
194202
impl ToOwned for str {
195203
type Owned = String;

src/librustc/cfg/construct.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
369369
hir::ExprKind::AddrOf(_, ref e) |
370370
hir::ExprKind::Cast(ref e, _) |
371371
hir::ExprKind::Type(ref e, _) |
372-
hir::ExprKind::Use(ref e) |
372+
hir::ExprKind::DropTemps(ref e) |
373373
hir::ExprKind::Unary(_, ref e) |
374374
hir::ExprKind::Field(ref e, _) |
375375
hir::ExprKind::Yield(ref e) |

src/librustc/hir/intravisit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,7 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr) {
10291029
visitor.visit_expr(subexpression);
10301030
visitor.visit_ty(typ)
10311031
}
1032-
ExprKind::Use(ref subexpression) => {
1032+
ExprKind::DropTemps(ref subexpression) => {
10331033
visitor.visit_expr(subexpression);
10341034
}
10351035
ExprKind::If(ref head_expression, ref if_block, ref optional_else) => {

src/librustc/hir/lowering.rs

+73-7
Original file line numberDiff line numberDiff line change
@@ -4671,7 +4671,7 @@ impl<'a> LoweringContext<'a> {
46714671
// The construct was introduced in #21984.
46724672
// FIXME(60253): Is this still necessary?
46734673
// Also, add the attributes to the outer returned expr node.
4674-
return self.expr_use(head_sp, match_expr, e.attrs.clone())
4674+
return self.expr_drop_temps(head_sp, match_expr, e.attrs.clone())
46754675
}
46764676

46774677
// Desugar `ExprKind::Try`
@@ -5030,15 +5030,19 @@ impl<'a> LoweringContext<'a> {
50305030
)
50315031
}
50325032

5033-
/// Wrap the given `expr` in `hir::ExprKind::Use`.
5033+
/// Wrap the given `expr` in a terminating scope using `hir::ExprKind::DropTemps`.
50345034
///
5035-
/// In terms of drop order, it has the same effect as
5036-
/// wrapping `expr` in `{ let _t = $expr; _t }` but
5037-
/// should provide better compile-time performance.
5035+
/// In terms of drop order, it has the same effect as wrapping `expr` in
5036+
/// `{ let _t = $expr; _t }` but should provide better compile-time performance.
50385037
///
50395038
/// The drop order can be important in e.g. `if expr { .. }`.
5040-
fn expr_use(&mut self, span: Span, expr: P<hir::Expr>, attrs: ThinVec<Attribute>) -> hir::Expr {
5041-
self.expr(span, hir::ExprKind::Use(expr), attrs)
5039+
fn expr_drop_temps(
5040+
&mut self,
5041+
span: Span,
5042+
expr: P<hir::Expr>,
5043+
attrs: ThinVec<Attribute>
5044+
) -> hir::Expr {
5045+
self.expr(span, hir::ExprKind::DropTemps(expr), attrs)
50425046
}
50435047

50445048
fn expr_match(
@@ -5400,3 +5404,65 @@ fn body_ids(bodies: &BTreeMap<hir::BodyId, hir::Body>) -> Vec<hir::BodyId> {
54005404
body_ids.sort_by_key(|b| bodies[b].value.span);
54015405
body_ids
54025406
}
5407+
5408+
/// Checks if the specified expression is a built-in range literal.
5409+
/// (See: `LoweringContext::lower_expr()`).
5410+
pub fn is_range_literal(sess: &Session, expr: &hir::Expr) -> bool {
5411+
use hir::{Path, QPath, ExprKind, TyKind};
5412+
5413+
// Returns whether the given path represents a (desugared) range,
5414+
// either in std or core, i.e. has either a `::std::ops::Range` or
5415+
// `::core::ops::Range` prefix.
5416+
fn is_range_path(path: &Path) -> bool {
5417+
let segs: Vec<_> = path.segments.iter().map(|seg| seg.ident.as_str().to_string()).collect();
5418+
let segs: Vec<_> = segs.iter().map(|seg| &**seg).collect();
5419+
5420+
// "{{root}}" is the equivalent of `::` prefix in `Path`.
5421+
if let ["{{root}}", std_core, "ops", range] = segs.as_slice() {
5422+
(*std_core == "std" || *std_core == "core") && range.starts_with("Range")
5423+
} else {
5424+
false
5425+
}
5426+
};
5427+
5428+
// Check whether a span corresponding to a range expression is a
5429+
// range literal, rather than an explicit struct or `new()` call.
5430+
fn is_lit(sess: &Session, span: &Span) -> bool {
5431+
let source_map = sess.source_map();
5432+
let end_point = source_map.end_point(*span);
5433+
5434+
if let Ok(end_string) = source_map.span_to_snippet(end_point) {
5435+
!(end_string.ends_with("}") || end_string.ends_with(")"))
5436+
} else {
5437+
false
5438+
}
5439+
};
5440+
5441+
match expr.node {
5442+
// All built-in range literals but `..=` and `..` desugar to `Struct`s.
5443+
ExprKind::Struct(ref qpath, _, _) => {
5444+
if let QPath::Resolved(None, ref path) = **qpath {
5445+
return is_range_path(&path) && is_lit(sess, &expr.span);
5446+
}
5447+
}
5448+
5449+
// `..` desugars to its struct path.
5450+
ExprKind::Path(QPath::Resolved(None, ref path)) => {
5451+
return is_range_path(&path) && is_lit(sess, &expr.span);
5452+
}
5453+
5454+
// `..=` desugars into `::std::ops::RangeInclusive::new(...)`.
5455+
ExprKind::Call(ref func, _) => {
5456+
if let ExprKind::Path(QPath::TypeRelative(ref ty, ref segment)) = func.node {
5457+
if let TyKind::Path(QPath::Resolved(None, ref path)) = ty.node {
5458+
let new_call = segment.ident.as_str() == "new";
5459+
return is_range_path(&path) && is_lit(sess, &expr.span) && new_call;
5460+
}
5461+
}
5462+
}
5463+
5464+
_ => {}
5465+
}
5466+
5467+
false
5468+
}

src/librustc/hir/mod.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -1366,7 +1366,7 @@ impl Expr {
13661366
ExprKind::Unary(..) => ExprPrecedence::Unary,
13671367
ExprKind::Lit(_) => ExprPrecedence::Lit,
13681368
ExprKind::Type(..) | ExprKind::Cast(..) => ExprPrecedence::Cast,
1369-
ExprKind::Use(ref expr, ..) => expr.precedence(),
1369+
ExprKind::DropTemps(ref expr, ..) => expr.precedence(),
13701370
ExprKind::If(..) => ExprPrecedence::If,
13711371
ExprKind::While(..) => ExprPrecedence::While,
13721372
ExprKind::Loop(..) => ExprPrecedence::Loop,
@@ -1438,7 +1438,7 @@ impl Expr {
14381438
ExprKind::Binary(..) |
14391439
ExprKind::Yield(..) |
14401440
ExprKind::Cast(..) |
1441-
ExprKind::Use(..) |
1441+
ExprKind::DropTemps(..) |
14421442
ExprKind::Err => {
14431443
false
14441444
}
@@ -1488,10 +1488,12 @@ pub enum ExprKind {
14881488
Cast(P<Expr>, P<Ty>),
14891489
/// A type reference (e.g., `Foo`).
14901490
Type(P<Expr>, P<Ty>),
1491-
/// Semantically equivalent to `{ let _t = expr; _t }`.
1492-
/// Maps directly to `hair::ExprKind::Use`.
1493-
/// Only exists to tweak the drop order in HIR.
1494-
Use(P<Expr>),
1491+
/// Wraps the expression in a terminating scope.
1492+
/// This makes it semantically equivalent to `{ let _t = expr; _t }`.
1493+
///
1494+
/// This construct only exists to tweak the drop order in HIR lowering.
1495+
/// An example of that is the desugaring of `for` loops.
1496+
DropTemps(P<Expr>),
14951497
/// An `if` block, with an optional else block.
14961498
///
14971499
/// I.e., `if <expr> { <expr> } else { <expr> }`.

src/librustc/hir/print.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1388,7 +1388,7 @@ impl<'a> State<'a> {
13881388
self.word_space(":")?;
13891389
self.print_type(&ty)?;
13901390
}
1391-
hir::ExprKind::Use(ref init) => {
1391+
hir::ExprKind::DropTemps(ref init) => {
13921392
// Print `{`:
13931393
self.cbox(indent_unit)?;
13941394
self.ibox(0)?;

src/librustc/middle/expr_use_visitor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
520520
self.consume_expr(&base);
521521
}
522522

523-
hir::ExprKind::Use(ref expr) => {
523+
hir::ExprKind::DropTemps(ref expr) => {
524524
self.consume_expr(&expr);
525525
}
526526

src/librustc/middle/liveness.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ fn visit_expr<'a, 'tcx>(ir: &mut IrMaps<'a, 'tcx>, expr: &'tcx Expr) {
521521
hir::ExprKind::Binary(..) |
522522
hir::ExprKind::AddrOf(..) |
523523
hir::ExprKind::Cast(..) |
524-
hir::ExprKind::Use(..) |
524+
hir::ExprKind::DropTemps(..) |
525525
hir::ExprKind::Unary(..) |
526526
hir::ExprKind::Break(..) |
527527
hir::ExprKind::Continue(_) |
@@ -1222,7 +1222,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
12221222
hir::ExprKind::AddrOf(_, ref e) |
12231223
hir::ExprKind::Cast(ref e, _) |
12241224
hir::ExprKind::Type(ref e, _) |
1225-
hir::ExprKind::Use(ref e) |
1225+
hir::ExprKind::DropTemps(ref e) |
12261226
hir::ExprKind::Unary(_, ref e) |
12271227
hir::ExprKind::Yield(ref e) |
12281228
hir::ExprKind::Repeat(ref e, _) => {
@@ -1526,7 +1526,7 @@ fn check_expr<'a, 'tcx>(this: &mut Liveness<'a, 'tcx>, expr: &'tcx Expr) {
15261526
hir::ExprKind::Match(..) | hir::ExprKind::While(..) | hir::ExprKind::Loop(..) |
15271527
hir::ExprKind::Index(..) | hir::ExprKind::Field(..) |
15281528
hir::ExprKind::Array(..) | hir::ExprKind::Tup(..) | hir::ExprKind::Binary(..) |
1529-
hir::ExprKind::Cast(..) | hir::ExprKind::Use(..) | hir::ExprKind::Unary(..) |
1529+
hir::ExprKind::Cast(..) | hir::ExprKind::DropTemps(..) | hir::ExprKind::Unary(..) |
15301530
hir::ExprKind::Ret(..) | hir::ExprKind::Break(..) | hir::ExprKind::Continue(..) |
15311531
hir::ExprKind::Lit(_) | hir::ExprKind::Block(..) | hir::ExprKind::AddrOf(..) |
15321532
hir::ExprKind::Struct(..) | hir::ExprKind::Repeat(..) |

src/librustc/middle/mem_categorization.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
677677
hir::ExprKind::Assign(..) | hir::ExprKind::AssignOp(..) |
678678
hir::ExprKind::Closure(..) | hir::ExprKind::Ret(..) |
679679
hir::ExprKind::Unary(..) | hir::ExprKind::Yield(..) |
680-
hir::ExprKind::MethodCall(..) | hir::ExprKind::Cast(..) | hir::ExprKind::Use(..) |
680+
hir::ExprKind::MethodCall(..) | hir::ExprKind::Cast(..) | hir::ExprKind::DropTemps(..) |
681681
hir::ExprKind::Array(..) | hir::ExprKind::Tup(..) | hir::ExprKind::If(..) |
682682
hir::ExprKind::Binary(..) | hir::ExprKind::While(..) |
683683
hir::ExprKind::Block(..) | hir::ExprKind::Loop(..) | hir::ExprKind::Match(..) |

src/librustc/middle/region.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -908,8 +908,8 @@ fn resolve_expr<'a, 'tcx>(visitor: &mut RegionResolutionVisitor<'a, 'tcx>, expr:
908908
visitor.cx.var_parent = visitor.cx.parent;
909909
}
910910

911-
hir::ExprKind::Use(ref expr) => {
912-
// `Use(expr)` does not denote a conditional scope.
911+
hir::ExprKind::DropTemps(ref expr) => {
912+
// `DropTemps(expr)` does not denote a conditional scope.
913913
// Rather, we want to achieve the same behavior as `{ let _t = expr; _t }`.
914914
terminating(expr.hir_id.local_id);
915915
}

0 commit comments

Comments
 (0)