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 10 pull requests #87908

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
ecb6686
Expand explanation of E0530
kornelski Aug 2, 2021
2a56a4f
removed references to parent/child from std::thread documentation
godmar Aug 7, 2021
eefd790
impl const From<num> for num
usbalbin Dec 27, 2020
09928a9
Add tests
usbalbin Mar 13, 2021
c8bf5ed
Add test for int to float
usbalbin Aug 7, 2021
d777cb8
less opt in const param of
BoxyUwU Aug 7, 2021
5f61271
fmt
BoxyUwU Aug 7, 2021
9a78489
Fix heading colours in Ayu theme
tsoutsman Aug 8, 2021
a4af040
Clarify terms in rustdoc book
tsoutsman Aug 8, 2021
f93cbed
Do not ICE on HIR based WF check when involving lifetimes
estebank Aug 6, 2021
cb19d83
Proper table row formatting in platform support
badboy Aug 9, 2021
bc4ce79
Added the `Option::unzip()` method
Kixiron Jul 30, 2021
eea3520
Added some basic tests for `Option::unzip()` and `Option::zip()` (I n…
Kixiron Jul 30, 2021
9d8081e
Enabled unzip_option feature for core tests & unzip docs
Kixiron Jul 31, 2021
ab2c590
Added tracking issue to unstable attribute
Kixiron Aug 5, 2021
c046d62
Use a more accurate span on assoc types WF checks
estebank Aug 6, 2021
33186e4
Rollup merge of #86840 - usbalbin:const_from, r=oli-obk
JohnTitor Aug 10, 2021
28e84e4
Rollup merge of #87636 - Kixiron:unzip-option, r=scottmcm
JohnTitor Aug 10, 2021
c9f68d2
Rollup merge of #87700 - kornelski:e530text, r=oli-obk
JohnTitor Aug 10, 2021
5709f22
Rollup merge of #87811 - estebank:issue-87549, r=oli-obk
JohnTitor Aug 10, 2021
304c5b9
Rollup merge of #87819 - estebank:assoc-type-span, r=jackh726
JohnTitor Aug 10, 2021
dd31fc3
Rollup merge of #87848 - godmar:@godmar/thread-join-documentation-fix…
JohnTitor Aug 10, 2021
be65d77
Rollup merge of #87854 - BoxyUwU:var-None, r=oli-obk
JohnTitor Aug 10, 2021
05d6699
Rollup merge of #87861 - tsoutsman:patch-1, r=GuillaumeGomez
JohnTitor Aug 10, 2021
91505d7
Rollup merge of #87865 - tsoutsman:master, r=GuillaumeGomez
JohnTitor Aug 10, 2021
74c27ab
Rollup merge of #87881 - badboy:platform-support-formatting, r=ehuss
JohnTitor Aug 10, 2021
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
53 changes: 39 additions & 14 deletions compiler/rustc_error_codes/src/error_codes/E0530.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,57 @@
A binding shadowed something it shouldn't.

Erroneous code example:
A match arm or a variable has a name that is already used by
something else, e.g.

* struct name
* enum variant
* static
* associated constant

This error may also happen when an enum variant *with fields* is used
in a pattern, but without its fields.

```compile_fail
enum Enum {
WithField(i32)
}

use Enum::*;
match WithField(1) {
WithField => {} // error: missing (_)
}
```

Match bindings cannot shadow statics:

```compile_fail,E0530
static TEST: i32 = 0;

let r: (i32, i32) = (0, 0);
let r = 123;
match r {
TEST => {} // error: match bindings cannot shadow statics
TEST => {} // error: name of a static
}
```

To fix this error, just change the binding's name in order to avoid shadowing
one of the following:
Fixed examples:

* struct name
* struct/enum variant
* static
* const
* associated const
```
static TEST: i32 = 0;

Fixed example:
let r = 123;
match r {
some_value => {} // ok!
}
```

or

```
static TEST: i32 = 0;
const TEST: i32 = 0; // const, not static

let r: (i32, i32) = (0, 0);
let r = 123;
match r {
something => {} // ok!
TEST => {} // const is ok!
other_values => {}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,10 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
if let ObligationCauseCode::WellFormed(Some(wf_loc)) =
root_obligation.cause.code.peel_derives()
{
if let Some(cause) =
self.tcx.diagnostic_hir_wf_check((obligation.predicate, wf_loc.clone()))
{
if let Some(cause) = self.tcx.diagnostic_hir_wf_check((
tcx.erase_regions(obligation.predicate),
wf_loc.clone(),
)) {
obligation.cause = cause;
span = obligation.cause.span;
}
Expand Down
18 changes: 10 additions & 8 deletions compiler/rustc_typeck/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,13 @@ pub fn check_trait_item(tcx: TyCtxt<'_>, def_id: LocalDefId) {
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
let trait_item = tcx.hir().expect_trait_item(hir_id);

let method_sig = match trait_item.kind {
hir::TraitItemKind::Fn(ref sig, _) => Some(sig),
_ => None,
let (method_sig, span) = match trait_item.kind {
hir::TraitItemKind::Fn(ref sig, _) => (Some(sig), trait_item.span),
hir::TraitItemKind::Type(_bounds, Some(ty)) => (None, ty.span),
_ => (None, trait_item.span),
};
check_object_unsafe_self_trait_by_name(tcx, &trait_item);
check_associated_item(tcx, trait_item.hir_id(), trait_item.span, method_sig);
check_associated_item(tcx, trait_item.hir_id(), span, method_sig);
}

fn could_be_self(trait_def_id: LocalDefId, ty: &hir::Ty<'_>) -> bool {
Expand Down Expand Up @@ -268,12 +269,13 @@ pub fn check_impl_item(tcx: TyCtxt<'_>, def_id: LocalDefId) {
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
let impl_item = tcx.hir().expect_impl_item(hir_id);

let method_sig = match impl_item.kind {
hir::ImplItemKind::Fn(ref sig, _) => Some(sig),
_ => None,
let (method_sig, span) = match impl_item.kind {
hir::ImplItemKind::Fn(ref sig, _) => (Some(sig), impl_item.span),
hir::ImplItemKind::TyAlias(ty) => (None, ty.span),
_ => (None, impl_item.span),
};

check_associated_item(tcx, impl_item.hir_id(), impl_item.span, method_sig);
check_associated_item(tcx, impl_item.hir_id(), span, method_sig);
}

fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) {
Expand Down
7 changes: 5 additions & 2 deletions compiler/rustc_typeck/src/collect/type_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,12 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<
// Try to use the segment resolution if it is valid, otherwise we
// default to the path resolution.
let res = segment.res.filter(|&r| r != Res::Err).unwrap_or(path.res);
use def::CtorOf;
let generics = match res {
Res::Def(DefKind::Ctor(..), def_id) => {
Res::Def(DefKind::Ctor(CtorOf::Variant, _), def_id) => tcx.generics_of(
tcx.parent(def_id).and_then(|def_id| tcx.parent(def_id)).unwrap(),
),
Res::Def(DefKind::Variant | DefKind::Ctor(CtorOf::Struct, _), def_id) => {
tcx.generics_of(tcx.parent(def_id).unwrap())
}
// Other `DefKind`s don't have generics and would ICE when calling
Expand All @@ -200,7 +204,6 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<
DefKind::Struct
| DefKind::Union
| DefKind::Enum
| DefKind::Variant
| DefKind::Trait
| DefKind::OpaqueTy
| DefKind::TyAlias
Expand Down
15 changes: 10 additions & 5 deletions library/core/src/convert/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ impl_float_to_int!(f64 => u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize);
macro_rules! impl_from {
($Small: ty, $Large: ty, #[$attr:meta], $doc: expr) => {
#[$attr]
impl From<$Small> for $Large {
#[rustc_const_unstable(feature = "const_num_from_num", issue = "87852")]
impl const From<$Small> for $Large {
// Rustdocs on the impl block show a "[+] show undocumented items" toggle.
// Rustdocs on functions do not.
#[doc = $doc]
Expand Down Expand Up @@ -172,7 +173,8 @@ impl_from! { f32, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0"
macro_rules! try_from_unbounded {
($source:ty, $($target:ty),*) => {$(
#[stable(feature = "try_from", since = "1.34.0")]
impl TryFrom<$source> for $target {
#[rustc_const_unstable(feature = "const_num_from_num", issue = "87852")]
impl const TryFrom<$source> for $target {
type Error = TryFromIntError;

/// Try to create the target number type from a source
Expand All @@ -190,7 +192,8 @@ macro_rules! try_from_unbounded {
macro_rules! try_from_lower_bounded {
($source:ty, $($target:ty),*) => {$(
#[stable(feature = "try_from", since = "1.34.0")]
impl TryFrom<$source> for $target {
#[rustc_const_unstable(feature = "const_num_from_num", issue = "87852")]
impl const TryFrom<$source> for $target {
type Error = TryFromIntError;

/// Try to create the target number type from a source
Expand All @@ -212,7 +215,8 @@ macro_rules! try_from_lower_bounded {
macro_rules! try_from_upper_bounded {
($source:ty, $($target:ty),*) => {$(
#[stable(feature = "try_from", since = "1.34.0")]
impl TryFrom<$source> for $target {
#[rustc_const_unstable(feature = "const_num_from_num", issue = "87852")]
impl const TryFrom<$source> for $target {
type Error = TryFromIntError;

/// Try to create the target number type from a source
Expand All @@ -234,7 +238,8 @@ macro_rules! try_from_upper_bounded {
macro_rules! try_from_both_bounded {
($source:ty, $($target:ty),*) => {$(
#[stable(feature = "try_from", since = "1.34.0")]
impl TryFrom<$source> for $target {
#[rustc_const_unstable(feature = "const_num_from_num", issue = "87852")]
impl const TryFrom<$source> for $target {
type Error = TryFromIntError;

/// Try to create the target number type from a source
Expand Down
1 change: 1 addition & 0 deletions library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
#![feature(const_slice_from_raw_parts)]
#![feature(const_slice_ptr_len)]
#![feature(const_swap)]
#![feature(const_trait_impl)]
#![feature(const_type_id)]
#![feature(const_type_name)]
#![feature(const_unreachable_unchecked)]
Expand Down
27 changes: 27 additions & 0 deletions library/core/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1399,6 +1399,33 @@ impl<T> Option<T> {
}
}

impl<T, U> Option<(T, U)> {
/// Unzips an option containing a tuple of two options
///
/// If `self` is `Some((a, b))` this method returns `(Some(a), Some(b))`.
/// Otherwise, `(None, None)` is returned.
///
/// # Examples
///
/// ```
/// #![feature(unzip_option)]
///
/// let x = Some((1, "hi"));
/// let y = None::<(u8, u32)>;
///
/// assert_eq!(x.unzip(), (Some(1), Some("hi")));
/// assert_eq!(y.unzip(), (None, None));
/// ```
#[inline]
#[unstable(feature = "unzip_option", issue = "87800", reason = "recently added")]
pub const fn unzip(self) -> (Option<T>, Option<U>) {
match self {
Some((a, b)) => (Some(a), Some(b)),
None => (None, None),
}
}
}

impl<T: Copy> Option<&T> {
/// Maps an `Option<&T>` to an `Option<T>` by copying the contents of the
/// option.
Expand Down
3 changes: 3 additions & 0 deletions library/core/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#![feature(const_ptr_read)]
#![feature(const_ptr_write)]
#![feature(const_ptr_offset)]
#![feature(const_trait_impl)]
#![feature(const_num_from_num)]
#![feature(core_intrinsics)]
#![feature(core_private_bignum)]
#![feature(core_private_diy_float)]
Expand Down Expand Up @@ -66,6 +68,7 @@
#![feature(slice_group_by)]
#![feature(trusted_random_access)]
#![feature(unsize)]
#![feature(unzip_option)]
#![deny(unsafe_op_in_unsafe_fn)]

extern crate test;
Expand Down
25 changes: 25 additions & 0 deletions library/core/tests/num/const_from.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#[test]
fn from() {
use core::convert::TryFrom;
use core::num::TryFromIntError;

// From
const FROM: i64 = i64::from(1i32);
assert_eq!(FROM, 1i64);

// From int to float
const FROM_F64: f64 = f64::from(42u8);
assert_eq!(FROM_F64, 42f64);

// Upper bounded
const U8_FROM_U16: Result<u8, TryFromIntError> = u8::try_from(1u16);
assert_eq!(U8_FROM_U16, Ok(1u8));

// Both bounded
const I8_FROM_I16: Result<i8, TryFromIntError> = i8::try_from(1i16);
assert_eq!(I8_FROM_I16, Ok(1i8));

// Lower bounded
const I16_FROM_U16: Result<i16, TryFromIntError> = i16::try_from(1u16);
assert_eq!(I16_FROM_U16, Ok(1i16));
}
2 changes: 2 additions & 0 deletions library/core/tests/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ mod u64;
mod u8;

mod bignum;

mod const_from;
mod dec2flt;
mod flt2dec;
mod int_log;
Expand Down
34 changes: 33 additions & 1 deletion library/core/tests/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,11 +399,43 @@ fn test_unwrap_drop() {
}

#[test]
pub fn option_ext() {
fn option_ext() {
let thing = "{{ f }}";
let f = thing.find("{{");

if f.is_none() {
println!("None!");
}
}

#[test]
fn zip_options() {
let x = Some(10);
let y = Some("foo");
let z: Option<usize> = None;

assert_eq!(x.zip(y), Some((10, "foo")));
assert_eq!(x.zip(z), None);
assert_eq!(z.zip(x), None);
}

#[test]
fn unzip_options() {
let x = Some((10, "foo"));
let y = None::<(bool, i32)>;

assert_eq!(x.unzip(), (Some(10), Some("foo")));
assert_eq!(y.unzip(), (None, None));
}

#[test]
fn zip_unzip_roundtrip() {
let x = Some(10);
let y = Some("foo");

let z = x.zip(y);
assert_eq!(z, Some((10, "foo")));

let a = z.unzip();
assert_eq!(a, (x, y));
}
Loading