Skip to content

Commit bf5b824

Browse files
authored
Auto merge of #37494 - GuillaumeGomez:rollup, r=GuillaumeGomez
Rollup of 5 pull requests - Successful merges: #37438, #37458, #37462, #37475, #37486 - Failed merges:
2 parents 074d30d + 0a59eba commit bf5b824

File tree

4 files changed

+112
-25
lines changed

4 files changed

+112
-25
lines changed

src/libcore/ops.rs

+68-19
Original file line numberDiff line numberDiff line change
@@ -1645,7 +1645,7 @@ rem_assign_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
16451645
#[lang = "bitand_assign"]
16461646
#[stable(feature = "op_assign_traits", since = "1.8.0")]
16471647
pub trait BitAndAssign<Rhs=Self> {
1648-
/// The method for the `&` operator
1648+
/// The method for the `&=` operator
16491649
#[stable(feature = "op_assign_traits", since = "1.8.0")]
16501650
fn bitand_assign(&mut self, Rhs);
16511651
}
@@ -1879,10 +1879,18 @@ shr_assign_impl_all! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }
18791879
/// The `Index` trait is used to specify the functionality of indexing operations
18801880
/// like `container[index]` when used in an immutable context.
18811881
///
1882+
/// `container[index]` is actually syntactic sugar for `*container.index(index)`,
1883+
/// but only when used as an immutable value. If a mutable value is requested,
1884+
/// [`IndexMut`] is used instead. This allows nice things such as
1885+
/// `let value = v[index]` if `value` implements [`Copy`].
1886+
///
1887+
/// [`IndexMut`]: ../../std/ops/trait.IndexMut.html
1888+
/// [`Copy`]: ../../std/marker/trait.Copy.html
1889+
///
18821890
/// # Examples
18831891
///
1884-
/// This example implements `Index` on a read-only `NucleotideCount` container,
1885-
/// enabling individual counts to be retrieved with index syntax.
1892+
/// The following example implements `Index` on a read-only `NucleotideCount`
1893+
/// container, enabling individual counts to be retrieved with index syntax.
18861894
///
18871895
/// ```
18881896
/// use std::ops::Index;
@@ -1934,37 +1942,78 @@ pub trait Index<Idx: ?Sized> {
19341942
}
19351943

19361944
/// The `IndexMut` trait is used to specify the functionality of indexing
1937-
/// operations like `container[index]`, when used in a mutable context.
1945+
/// operations like `container[index]` when used in a mutable context.
1946+
///
1947+
/// `container[index]` is actually syntactic sugar for
1948+
/// `*container.index_mut(index)`, but only when used as a mutable value. If
1949+
/// an immutable value is requested, the [`Index`] trait is used instead. This
1950+
/// allows nice things such as `v[index] = value` if `value` implements [`Copy`].
1951+
///
1952+
/// [`Index`]: ../../std/ops/trait.Index.html
1953+
/// [`Copy`]: ../../std/marker/trait.Copy.html
19381954
///
19391955
/// # Examples
19401956
///
1941-
/// A trivial implementation of `IndexMut` for a type `Foo`. When `&mut Foo[2]`
1942-
/// happens, it ends up calling `index_mut`, and therefore, `main` prints
1943-
/// `Mutable indexing with 2!`.
1957+
/// A very simple implementation of a `Balance` struct that has two sides, where
1958+
/// each can be indexed mutably and immutably.
19441959
///
19451960
/// ```
1946-
/// use std::ops::{Index, IndexMut};
1961+
/// use std::ops::{Index,IndexMut};
19471962
///
1948-
/// #[derive(Copy, Clone)]
1949-
/// struct Foo;
1963+
/// #[derive(Debug)]
1964+
/// enum Side {
1965+
/// Left,
1966+
/// Right,
1967+
/// }
19501968
///
1951-
/// impl Index<usize> for Foo {
1952-
/// type Output = Foo;
1969+
/// #[derive(Debug, PartialEq)]
1970+
/// enum Weight {
1971+
/// Kilogram(f32),
1972+
/// Pound(f32),
1973+
/// }
1974+
///
1975+
/// struct Balance {
1976+
/// pub left: Weight,
1977+
/// pub right:Weight,
1978+
/// }
19531979
///
1954-
/// fn index(&self, _index: usize) -> &Foo {
1955-
/// self
1980+
/// impl Index<Side> for Balance {
1981+
/// type Output = Weight;
1982+
///
1983+
/// fn index<'a>(&'a self, index: Side) -> &'a Weight {
1984+
/// println!("Accessing {:?}-side of balance immutably", index);
1985+
/// match index {
1986+
/// Side::Left => &self.left,
1987+
/// Side::Right => &self.right,
1988+
/// }
19561989
/// }
19571990
/// }
19581991
///
1959-
/// impl IndexMut<usize> for Foo {
1960-
/// fn index_mut(&mut self, index: usize) -> &mut Foo {
1961-
/// println!("Mutable indexing with {}!", index);
1962-
/// self
1992+
/// impl IndexMut<Side> for Balance {
1993+
/// fn index_mut<'a>(&'a mut self, index: Side) -> &'a mut Weight {
1994+
/// println!("Accessing {:?}-side of balance mutably", index);
1995+
/// match index {
1996+
/// Side::Left => &mut self.left,
1997+
/// Side::Right => &mut self.right,
1998+
/// }
19631999
/// }
19642000
/// }
19652001
///
19662002
/// fn main() {
1967-
/// &mut Foo[2];
2003+
/// let mut balance = Balance {
2004+
/// right: Weight::Kilogram(2.5),
2005+
/// left: Weight::Pound(1.5),
2006+
/// };
2007+
///
2008+
/// // In this case balance[Side::Right] is sugar for
2009+
/// // *balance.index(Side::Right), since we are only reading
2010+
/// // balance[Side::Right], not writing it.
2011+
/// assert_eq!(balance[Side::Right],Weight::Kilogram(2.5));
2012+
///
2013+
/// // However in this case balance[Side::Left] is sugar for
2014+
/// // *balance.index_mut(Side::Left), since we are writing
2015+
/// // balance[Side::Left].
2016+
/// balance[Side::Left] = Weight::Kilogram(3.0);
19682017
/// }
19692018
/// ```
19702019
#[lang = "index_mut"]

src/librustc_privacy/lib.rs

-3
Original file line numberDiff line numberDiff line change
@@ -859,9 +859,6 @@ impl<'a, 'tcx, 'v> Visitor<'v> for ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx>
859859
// expression/block context can't possibly contain exported things.
860860
// (Making them no-ops stops us from traversing the whole AST without
861861
// having to be super careful about our `walk_...` calls above.)
862-
// FIXME(#29524): Unfortunately this ^^^ is not true, blocks can contain
863-
// exported items (e.g. impls) and actual code in rustc itself breaks
864-
// if we don't traverse blocks in `EmbargoVisitor`
865862
fn visit_block(&mut self, _: &hir::Block) {}
866863
fn visit_expr(&mut self, _: &hir::Expr) {}
867864
}

src/librustc_resolve/diagnostics.rs

+41-1
Original file line numberDiff line numberDiff line change
@@ -1461,6 +1461,47 @@ match r {
14611461
```
14621462
"##,
14631463

1464+
E0532: r##"
1465+
Pattern arm did not match expected kind.
1466+
1467+
Erroneous code example:
1468+
1469+
```compile_fail,E0532
1470+
enum State {
1471+
Succeeded,
1472+
Failed(String),
1473+
}
1474+
1475+
fn print_on_failure(state: &State) {
1476+
match *state {
1477+
// error: expected unit struct/variant or constant, found tuple
1478+
// variant `State::Failed`
1479+
State::Failed => println!("Failed"),
1480+
_ => ()
1481+
}
1482+
}
1483+
```
1484+
1485+
To fix this error, ensure the match arm kind is the same as the expression
1486+
matched.
1487+
1488+
Fixed example:
1489+
1490+
```
1491+
enum State {
1492+
Succeeded,
1493+
Failed(String),
1494+
}
1495+
1496+
fn print_on_failure(state: &State) {
1497+
match *state {
1498+
State::Failed(ref msg) => println!("Failed with {}", msg),
1499+
_ => ()
1500+
}
1501+
}
1502+
```
1503+
"##,
1504+
14641505
}
14651506

14661507
register_diagnostics! {
@@ -1480,6 +1521,5 @@ register_diagnostics! {
14801521
// E0421, merged into 531
14811522
// E0422, merged into 531/532
14821523
E0531, // unresolved pattern path kind `name`
1483-
E0532, // expected pattern path kind, found another pattern path kind
14841524
// E0427, merged into 530
14851525
}

src/libsyntax_ext/deriving/generic/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1546,7 +1546,7 @@ impl<'a> TraitDef<'a> {
15461546
cx.span_bug(sp, "a braced struct with unnamed fields in `derive`");
15471547
}
15481548
codemap::Spanned {
1549-
span: pat.span,
1549+
span: Span { expn_id: self.span.expn_id, ..pat.span },
15501550
node: ast::FieldPat {
15511551
ident: ident.unwrap(),
15521552
pat: pat,
@@ -1577,7 +1577,8 @@ impl<'a> TraitDef<'a> {
15771577
mutbl: ast::Mutability)
15781578
-> (P<ast::Pat>, Vec<(Span, Option<Ident>, P<Expr>, &'a [ast::Attribute])>) {
15791579
let variant_ident = variant.node.name;
1580-
let variant_path = cx.path(variant.span, vec![enum_ident, variant_ident]);
1580+
let sp = Span { expn_id: self.span.expn_id, ..variant.span };
1581+
let variant_path = cx.path(sp, vec![enum_ident, variant_ident]);
15811582
self.create_struct_pattern(cx, variant_path, &variant.node.data, prefix, mutbl)
15821583
}
15831584
}

0 commit comments

Comments
 (0)