Skip to content

Commit e8e1b32

Browse files
committed
Auto merge of rust-lang#87923 - JohnTitor:rollup-id54fyz, r=JohnTitor
Rollup of 14 pull requests Successful merges: - rust-lang#86840 (Constify implementations of `(Try)From` for int types) - rust-lang#87582 (Implement `Printer` for `&mut SymbolPrinter`) - rust-lang#87636 (Added the `Option::unzip()` method) - rust-lang#87700 (Expand explanation of E0530) - rust-lang#87811 (Do not ICE on HIR based WF check when involving lifetimes) - rust-lang#87848 (removed references to parent/child from std::thread documentation) - rust-lang#87854 (correctly handle enum variants in `opt_const_param_of`) - rust-lang#87861 (Fix heading colours in Ayu theme) - rust-lang#87865 (Clarify terms in rustdoc book) - rust-lang#87876 (add `windows` count test) - rust-lang#87880 (Remove duplicate trait bounds in `rustc_data_structures::graph`) - rust-lang#87881 (Proper table row formatting in platform support) - rust-lang#87889 (Use smaller spans when suggesting method call disambiguation) - rust-lang#87895 (typeck: don't suggest inaccessible fields in struct literals and suggest ignoring inaccessible fields in struct patterns) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents ae90dcf + 4be63b2 commit e8e1b32

File tree

32 files changed

+399
-132
lines changed

32 files changed

+399
-132
lines changed

compiler/rustc_data_structures/src/graph/mod.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,13 @@ pub trait WithStartNode: DirectedGraph {
6060
}
6161

6262
pub trait ControlFlowGraph:
63-
DirectedGraph + WithStartNode + WithPredecessors + WithStartNode + WithSuccessors + WithNumNodes
63+
DirectedGraph + WithStartNode + WithPredecessors + WithSuccessors + WithNumNodes
6464
{
6565
// convenient trait
6666
}
6767

6868
impl<T> ControlFlowGraph for T where
69-
T: DirectedGraph
70-
+ WithStartNode
71-
+ WithPredecessors
72-
+ WithStartNode
73-
+ WithSuccessors
74-
+ WithNumNodes
69+
T: DirectedGraph + WithStartNode + WithPredecessors + WithSuccessors + WithNumNodes
7570
{
7671
}
7772

Original file line numberDiff line numberDiff line change
@@ -1,32 +1,57 @@
11
A binding shadowed something it shouldn't.
22

3-
Erroneous code example:
3+
A match arm or a variable has a name that is already used by
4+
something else, e.g.
5+
6+
* struct name
7+
* enum variant
8+
* static
9+
* associated constant
10+
11+
This error may also happen when an enum variant *with fields* is used
12+
in a pattern, but without its fields.
13+
14+
```compile_fail
15+
enum Enum {
16+
WithField(i32)
17+
}
18+
19+
use Enum::*;
20+
match WithField(1) {
21+
WithField => {} // error: missing (_)
22+
}
23+
```
24+
25+
Match bindings cannot shadow statics:
426

527
```compile_fail,E0530
628
static TEST: i32 = 0;
729
8-
let r: (i32, i32) = (0, 0);
30+
let r = 123;
931
match r {
10-
TEST => {} // error: match bindings cannot shadow statics
32+
TEST => {} // error: name of a static
1133
}
1234
```
1335

14-
To fix this error, just change the binding's name in order to avoid shadowing
15-
one of the following:
36+
Fixed examples:
1637

17-
* struct name
18-
* struct/enum variant
19-
* static
20-
* const
21-
* associated const
38+
```
39+
static TEST: i32 = 0;
2240
23-
Fixed example:
41+
let r = 123;
42+
match r {
43+
some_value => {} // ok!
44+
}
45+
```
46+
47+
or
2448

2549
```
26-
static TEST: i32 = 0;
50+
const TEST: i32 = 0; // const, not static
2751
28-
let r: (i32, i32) = (0, 0);
52+
let r = 123;
2953
match r {
30-
something => {} // ok!
54+
TEST => {} // const is ok!
55+
other_values => {}
3156
}
3257
```

compiler/rustc_symbol_mangling/src/legacy.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ pub(super) fn mangle(
5555

5656
let hash = get_symbol_hash(tcx, instance, instance_ty, instantiating_crate);
5757

58-
let mut printer = SymbolPrinter { tcx, path: SymbolPath::new(), keep_within_component: false }
58+
let mut printer = SymbolPrinter { tcx, path: SymbolPath::new(), keep_within_component: false };
59+
printer
5960
.print_def_path(
6061
def_id,
6162
if let ty::InstanceDef::DropGlue(_, _) = instance.def {
@@ -198,7 +199,7 @@ struct SymbolPrinter<'tcx> {
198199
// `PrettyPrinter` aka pretty printing of e.g. types in paths,
199200
// symbol names should have their own printing machinery.
200201

201-
impl Printer<'tcx> for SymbolPrinter<'tcx> {
202+
impl Printer<'tcx> for &mut SymbolPrinter<'tcx> {
202203
type Error = fmt::Error;
203204

204205
type Path = Self;
@@ -242,7 +243,7 @@ impl Printer<'tcx> for SymbolPrinter<'tcx> {
242243
Ok(self)
243244
}
244245

245-
fn print_const(mut self, ct: &'tcx ty::Const<'tcx>) -> Result<Self::Const, Self::Error> {
246+
fn print_const(self, ct: &'tcx ty::Const<'tcx>) -> Result<Self::Const, Self::Error> {
246247
// only print integers
247248
if let ty::ConstKind::Value(ConstValue::Scalar(Scalar::Int { .. })) = ct.val {
248249
if ct.ty.is_integral() {
@@ -253,7 +254,7 @@ impl Printer<'tcx> for SymbolPrinter<'tcx> {
253254
Ok(self)
254255
}
255256

256-
fn path_crate(mut self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
257+
fn path_crate(self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
257258
self.write_str(&self.tcx.crate_name(cnum).as_str())?;
258259
Ok(self)
259260
}
@@ -344,7 +345,7 @@ impl Printer<'tcx> for SymbolPrinter<'tcx> {
344345
}
345346
}
346347

347-
impl PrettyPrinter<'tcx> for SymbolPrinter<'tcx> {
348+
impl PrettyPrinter<'tcx> for &mut SymbolPrinter<'tcx> {
348349
fn region_should_not_be_omitted(&self, _region: ty::Region<'_>) -> bool {
349350
false
350351
}

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,10 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
245245
if let ObligationCauseCode::WellFormed(Some(wf_loc)) =
246246
root_obligation.cause.code.peel_derives()
247247
{
248-
if let Some(cause) =
249-
self.tcx.diagnostic_hir_wf_check((obligation.predicate, wf_loc.clone()))
250-
{
248+
if let Some(cause) = self.tcx.diagnostic_hir_wf_check((
249+
tcx.erase_regions(obligation.predicate),
250+
wf_loc.clone(),
251+
)) {
251252
obligation.cause = cause;
252253
span = obligation.cause.span;
253254
}

compiler/rustc_typeck/src/check/expr.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -1313,15 +1313,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13131313
.emit();
13141314
}
13151315
} else if check_completeness && !error_happened && !remaining_fields.is_empty() {
1316-
let no_accessible_remaining_fields = remaining_fields
1317-
.iter()
1318-
.find(|(_, (_, field))| {
1319-
field.vis.is_accessible_from(tcx.parent_module(expr_id).to_def_id(), tcx)
1320-
})
1321-
.is_none();
1316+
let inaccessible_remaining_fields = remaining_fields.iter().any(|(_, (_, field))| {
1317+
!field.vis.is_accessible_from(tcx.parent_module(expr_id).to_def_id(), tcx)
1318+
});
13221319

1323-
if no_accessible_remaining_fields {
1324-
self.report_no_accessible_fields(adt_ty, span);
1320+
if inaccessible_remaining_fields {
1321+
self.report_inaccessible_fields(adt_ty, span);
13251322
} else {
13261323
self.report_missing_fields(adt_ty, span, remaining_fields);
13271324
}
@@ -1398,7 +1395,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13981395
.emit();
13991396
}
14001397

1401-
/// Report an error for a struct field expression when there are no visible fields.
1398+
/// Report an error for a struct field expression when there are invisible fields.
14021399
///
14031400
/// ```text
14041401
/// error: cannot construct `Foo` with struct literal syntax due to inaccessible fields
@@ -1409,7 +1406,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14091406
///
14101407
/// error: aborting due to previous error
14111408
/// ```
1412-
fn report_no_accessible_fields(&self, adt_ty: Ty<'tcx>, span: Span) {
1409+
fn report_inaccessible_fields(&self, adt_ty: Ty<'tcx>, span: Span) {
14131410
self.tcx.sess.span_err(
14141411
span,
14151412
&format!(

compiler/rustc_typeck/src/check/method/suggest.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1695,8 +1695,8 @@ fn print_disambiguation_help(
16951695
source_map: &source_map::SourceMap,
16961696
) {
16971697
let mut applicability = Applicability::MachineApplicable;
1698-
let sugg_args = if let (ty::AssocKind::Fn, Some(args)) = (kind, args) {
1699-
format!(
1698+
let (span, sugg) = if let (ty::AssocKind::Fn, Some(args)) = (kind, args) {
1699+
let args = format!(
17001700
"({}{})",
17011701
if rcvr_ty.is_region_ptr() {
17021702
if rcvr_ty.is_mutable_ptr() { "&mut " } else { "&" }
@@ -1710,12 +1710,12 @@ fn print_disambiguation_help(
17101710
}))
17111711
.collect::<Vec<_>>()
17121712
.join(", "),
1713-
)
1713+
);
1714+
(span, format!("{}::{}{}", trait_name, item_name, args))
17141715
} else {
1715-
String::new()
1716+
(span.with_hi(item_name.span.lo()), format!("{}::", trait_name))
17161717
};
1717-
let sugg = format!("{}::{}{}", trait_name, item_name, sugg_args);
1718-
err.span_suggestion(
1718+
err.span_suggestion_verbose(
17191719
span,
17201720
&format!(
17211721
"disambiguate the {} for {}",

compiler/rustc_typeck/src/check/pat.rs

+22-10
Original file line numberDiff line numberDiff line change
@@ -1250,15 +1250,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12501250
tcx.sess.struct_span_err(pat.span, "`..` cannot be used in union patterns").emit();
12511251
}
12521252
} else if !etc && !unmentioned_fields.is_empty() {
1253-
let no_accessible_unmentioned_fields = !unmentioned_fields.iter().any(|(field, _)| {
1254-
field.vis.is_accessible_from(tcx.parent_module(pat.hir_id).to_def_id(), tcx)
1255-
});
1253+
let accessible_unmentioned_fields: Vec<_> = unmentioned_fields
1254+
.iter()
1255+
.copied()
1256+
.filter(|(field, _)| {
1257+
field.vis.is_accessible_from(tcx.parent_module(pat.hir_id).to_def_id(), tcx)
1258+
})
1259+
.collect();
12561260

1257-
if no_accessible_unmentioned_fields {
1261+
if accessible_unmentioned_fields.is_empty() {
12581262
unmentioned_err = Some(self.error_no_accessible_fields(pat, &fields));
12591263
} else {
1260-
unmentioned_err =
1261-
Some(self.error_unmentioned_fields(pat, &unmentioned_fields, &fields));
1264+
unmentioned_err = Some(self.error_unmentioned_fields(
1265+
pat,
1266+
&accessible_unmentioned_fields,
1267+
accessible_unmentioned_fields.len() != unmentioned_fields.len(),
1268+
&fields,
1269+
));
12621270
}
12631271
}
12641272
match (inexistent_fields_err, unmentioned_err) {
@@ -1583,17 +1591,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
15831591
&self,
15841592
pat: &Pat<'_>,
15851593
unmentioned_fields: &[(&ty::FieldDef, Ident)],
1594+
have_inaccessible_fields: bool,
15861595
fields: &'tcx [hir::PatField<'tcx>],
15871596
) -> DiagnosticBuilder<'tcx> {
1597+
let inaccessible = if have_inaccessible_fields { " and inaccessible fields" } else { "" };
15881598
let field_names = if unmentioned_fields.len() == 1 {
1589-
format!("field `{}`", unmentioned_fields[0].1)
1599+
format!("field `{}`{}", unmentioned_fields[0].1, inaccessible)
15901600
} else {
15911601
let fields = unmentioned_fields
15921602
.iter()
15931603
.map(|(_, name)| format!("`{}`", name))
15941604
.collect::<Vec<String>>()
15951605
.join(", ");
1596-
format!("fields {}", fields)
1606+
format!("fields {}{}", fields, inaccessible)
15971607
};
15981608
let mut err = struct_span_err!(
15991609
self.tcx.sess,
@@ -1624,17 +1634,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
16241634
err.span_suggestion(
16251635
sp,
16261636
&format!(
1627-
"include the missing field{} in the pattern",
1637+
"include the missing field{} in the pattern{}",
16281638
if len == 1 { "" } else { "s" },
1639+
if have_inaccessible_fields { " and ignore the inaccessible fields" } else { "" }
16291640
),
16301641
format!(
1631-
"{}{}{}",
1642+
"{}{}{}{}",
16321643
prefix,
16331644
unmentioned_fields
16341645
.iter()
16351646
.map(|(_, name)| name.to_string())
16361647
.collect::<Vec<_>>()
16371648
.join(", "),
1649+
if have_inaccessible_fields { ", .." } else { "" },
16381650
postfix,
16391651
),
16401652
Applicability::MachineApplicable,

compiler/rustc_typeck/src/collect/type_of.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,12 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<
190190
// Try to use the segment resolution if it is valid, otherwise we
191191
// default to the path resolution.
192192
let res = segment.res.filter(|&r| r != Res::Err).unwrap_or(path.res);
193+
use def::CtorOf;
193194
let generics = match res {
194-
Res::Def(DefKind::Ctor(..), def_id) => {
195+
Res::Def(DefKind::Ctor(CtorOf::Variant, _), def_id) => tcx.generics_of(
196+
tcx.parent(def_id).and_then(|def_id| tcx.parent(def_id)).unwrap(),
197+
),
198+
Res::Def(DefKind::Variant | DefKind::Ctor(CtorOf::Struct, _), def_id) => {
195199
tcx.generics_of(tcx.parent(def_id).unwrap())
196200
}
197201
// Other `DefKind`s don't have generics and would ICE when calling
@@ -200,7 +204,6 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<
200204
DefKind::Struct
201205
| DefKind::Union
202206
| DefKind::Enum
203-
| DefKind::Variant
204207
| DefKind::Trait
205208
| DefKind::OpaqueTy
206209
| DefKind::TyAlias

library/core/src/convert/num.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ impl_float_to_int!(f64 => u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize);
4545
macro_rules! impl_from {
4646
($Small: ty, $Large: ty, #[$attr:meta], $doc: expr) => {
4747
#[$attr]
48-
impl From<$Small> for $Large {
48+
#[rustc_const_unstable(feature = "const_num_from_num", issue = "87852")]
49+
impl const From<$Small> for $Large {
4950
// Rustdocs on the impl block show a "[+] show undocumented items" toggle.
5051
// Rustdocs on functions do not.
5152
#[doc = $doc]
@@ -172,7 +173,8 @@ impl_from! { f32, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0"
172173
macro_rules! try_from_unbounded {
173174
($source:ty, $($target:ty),*) => {$(
174175
#[stable(feature = "try_from", since = "1.34.0")]
175-
impl TryFrom<$source> for $target {
176+
#[rustc_const_unstable(feature = "const_num_from_num", issue = "87852")]
177+
impl const TryFrom<$source> for $target {
176178
type Error = TryFromIntError;
177179

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

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

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

240245
/// Try to create the target number type from a source

library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
#![feature(const_slice_from_raw_parts)]
100100
#![feature(const_slice_ptr_len)]
101101
#![feature(const_swap)]
102+
#![feature(const_trait_impl)]
102103
#![feature(const_type_id)]
103104
#![feature(const_type_name)]
104105
#![feature(const_unreachable_unchecked)]

0 commit comments

Comments
 (0)