Skip to content

Commit e495b37

Browse files
committed
Auto merge of #102867 - JohnTitor:rollup-qnwsajt, r=JohnTitor
Rollup of 6 pull requests Successful merges: - #102275 (Stabilize `half_open_range_patterns`) - #102323 (Trying to suggest additional lifetime parameter) - #102345 (Recover from impl Trait in type param bound) - #102845 (Elaborate trait ref to compute object safety.) - #102860 (Add missing documentation for FileNameDisplayPreference variants) - #102862 (From<Alignment> for usize & NonZeroUsize) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 1a7c203 + 6d35efe commit e495b37

File tree

65 files changed

+507
-442
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+507
-442
lines changed

compiler/rustc_ast_passes/src/feature_gate.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
461461
if let PatKind::Range(Some(_), None, Spanned { .. }) = inner_pat.kind {
462462
gate_feature_post!(
463463
&self,
464-
half_open_range_patterns,
464+
half_open_range_patterns_in_slices,
465465
pat.span,
466466
"`X..` patterns in slices are experimental"
467467
);
@@ -589,7 +589,10 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session) {
589589
gate_all!(generators, "yield syntax is experimental");
590590
gate_all!(raw_ref_op, "raw address of syntax is experimental");
591591
gate_all!(const_trait_impl, "const trait impls are experimental");
592-
gate_all!(half_open_range_patterns, "half-open range patterns are unstable");
592+
gate_all!(
593+
half_open_range_patterns_in_slices,
594+
"half-open range patterns in slices are unstable"
595+
);
593596
gate_all!(inline_const, "inline-const is experimental");
594597
gate_all!(inline_const_pat, "inline-const in pattern position is experimental");
595598
gate_all!(associated_const_equality, "associated const equality is incomplete");

compiler/rustc_feature/src/accepted.rs

+2
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ declare_features! (
169169
(accepted, global_allocator, "1.28.0", Some(27389), None),
170170
// FIXME: explain `globs`.
171171
(accepted, globs, "1.0.0", None, None),
172+
/// Allows using `..=X` as a pattern.
173+
(accepted, half_open_range_patterns, "CURRENT_RUSTC_VERSION", Some(67264), None),
172174
/// Allows using the `u128` and `i128` types.
173175
(accepted, i128_type, "1.26.0", Some(35118), None),
174176
/// Allows the use of `if let` expressions.

compiler/rustc_feature/src/active.rs

+2
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,8 @@ declare_features! (
414414
(incomplete, generic_const_exprs, "1.56.0", Some(76560), None),
415415
/// Allows using `..X`, `..=X`, `...X`, and `X..` as a pattern.
416416
(active, half_open_range_patterns, "1.41.0", Some(67264), None),
417+
/// Allows using `..=X` as a patterns in slices.
418+
(active, half_open_range_patterns_in_slices, "CURRENT_RUSTC_VERSION", Some(67264), None),
417419
/// Allows `if let` guard in match arms.
418420
(active, if_let_guard, "1.47.0", Some(51114), None),
419421
/// Allows using imported `main` function

compiler/rustc_middle/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
#![feature(type_alias_impl_trait)]
4545
#![feature(associated_type_bounds)]
4646
#![feature(rustc_attrs)]
47-
#![feature(half_open_range_patterns)]
47+
#![cfg_attr(bootstrap, feature(half_open_range_patterns))]
4848
#![feature(control_flow_enum)]
4949
#![feature(associated_type_defaults)]
5050
#![feature(trusted_step)]

compiler/rustc_parse/src/parser/generics.rs

+34-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use super::{ForceCollect, Parser, TrailingToken};
22

33
use rustc_ast::token;
4-
use rustc_ast::{self as ast, AttrVec, GenericBounds, GenericParam, GenericParamKind, WhereClause};
4+
use rustc_ast::{
5+
self as ast, AttrVec, GenericBounds, GenericParam, GenericParamKind, TyKind, WhereClause,
6+
};
57
use rustc_errors::{Applicability, PResult};
68
use rustc_span::symbol::kw;
79

@@ -31,13 +33,43 @@ impl<'a> Parser<'a> {
3133
let mut colon_span = None;
3234
let bounds = if self.eat(&token::Colon) {
3335
colon_span = Some(self.prev_token.span);
36+
// recover from `impl Trait` in type param bound
37+
if self.token.is_keyword(kw::Impl) {
38+
let impl_span = self.token.span;
39+
let snapshot = self.create_snapshot_for_diagnostic();
40+
match self.parse_ty() {
41+
Ok(p) => {
42+
if let TyKind::ImplTrait(_, bounds) = &(*p).kind {
43+
let span = impl_span.to(self.token.span.shrink_to_lo());
44+
let mut err = self.struct_span_err(
45+
span,
46+
"expected trait bound, found `impl Trait` type",
47+
);
48+
err.span_label(span, "not a trait");
49+
if let [bound, ..] = &bounds[..] {
50+
err.span_suggestion_verbose(
51+
impl_span.until(bound.span()),
52+
"use the trait bounds directly",
53+
String::new(),
54+
Applicability::MachineApplicable,
55+
);
56+
}
57+
err.emit();
58+
return Err(err);
59+
}
60+
}
61+
Err(err) => {
62+
err.cancel();
63+
}
64+
}
65+
self.restore_snapshot(snapshot);
66+
}
3467
self.parse_generic_bounds(colon_span)?
3568
} else {
3669
Vec::new()
3770
};
3871

3972
let default = if self.eat(&token::Eq) { Some(self.parse_ty()?) } else { None };
40-
4173
Ok(GenericParam {
4274
ident,
4375
id: ast::DUMMY_NODE_ID,

compiler/rustc_parse/src/parser/pat.rs

-1
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,6 @@ impl<'a> Parser<'a> {
777777
/// expression syntax `...expr` for splatting in expressions.
778778
fn parse_pat_range_to(&mut self, mut re: Spanned<RangeEnd>) -> PResult<'a, PatKind> {
779779
let end = self.parse_pat_range_end()?;
780-
self.sess.gated_spans.gate(sym::half_open_range_patterns, re.span.to(self.prev_token.span));
781780
if let RangeEnd::Included(ref mut syn @ RangeSyntax::DotDotDot) = &mut re.node {
782781
*syn = RangeSyntax::DotDotEq;
783782
self.struct_span_err(re.span, "range-to patterns with `...` are not allowed")

compiler/rustc_resolve/src/late/lifetimes.rs

+31-2
Original file line numberDiff line numberDiff line change
@@ -1333,12 +1333,41 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
13331333
&& let hir::IsAsync::NotAsync = self.tcx.asyncness(lifetime_ref.hir_id.owner.def_id)
13341334
&& !self.tcx.features().anonymous_lifetime_in_impl_trait
13351335
{
1336-
rustc_session::parse::feature_err(
1336+
let mut diag = rustc_session::parse::feature_err(
13371337
&self.tcx.sess.parse_sess,
13381338
sym::anonymous_lifetime_in_impl_trait,
13391339
lifetime_ref.span,
13401340
"anonymous lifetimes in `impl Trait` are unstable",
1341-
).emit();
1341+
);
1342+
1343+
match self.tcx.hir().get_generics(lifetime_ref.hir_id.owner.def_id) {
1344+
Some(generics) => {
1345+
1346+
let new_param_sugg_tuple;
1347+
1348+
new_param_sugg_tuple = match generics.span_for_param_suggestion() {
1349+
Some(_) => {
1350+
Some((self.tcx.sess.source_map().span_through_char(generics.span, '<').shrink_to_hi(), "'a, ".to_owned()))
1351+
},
1352+
None => Some((generics.span, "<'a>".to_owned()))
1353+
};
1354+
1355+
let mut multi_sugg_vec = vec![(lifetime_ref.span.shrink_to_hi(), "'a ".to_owned())];
1356+
1357+
if let Some(new_tuple) = new_param_sugg_tuple{
1358+
multi_sugg_vec.push(new_tuple);
1359+
}
1360+
1361+
diag.span_label(lifetime_ref.span, "expected named lifetime parameter");
1362+
diag.multipart_suggestion("consider introducing a named lifetime parameter",
1363+
multi_sugg_vec,
1364+
rustc_errors::Applicability::MaybeIncorrect);
1365+
1366+
},
1367+
None => { }
1368+
}
1369+
1370+
diag.emit();
13421371
return;
13431372
}
13441373
scope = s;

compiler/rustc_span/src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,11 @@ impl From<PathBuf> for FileName {
298298

299299
#[derive(Clone, Copy, Eq, PartialEq, Hash, Debug)]
300300
pub enum FileNameDisplayPreference {
301+
/// Display the path after the application of rewrite rules provided via `--remap-path-prefix`.
302+
/// This is appropriate for paths that get embedded into files produced by the compiler.
301303
Remapped,
304+
/// Display the path before the application of rewrite rules provided via `--remap-path-prefix`.
305+
/// This is appropriate for use in user-facing output (such as diagnostics).
302306
Local,
303307
}
304308

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,7 @@ symbols! {
785785
globs,
786786
gt,
787787
half_open_range_patterns,
788+
half_open_range_patterns_in_slices,
788789
hash,
789790
hexagon_target_feature,
790791
hidden,

compiler/rustc_trait_selection/src/traits/object_safety.rs

+15-29
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//! - not reference the erased type `Self` except for in this receiver;
99
//! - not have generic type parameters.
1010
11-
use super::elaborate_predicates;
11+
use super::{elaborate_predicates, elaborate_trait_ref};
1212

1313
use crate::infer::TyCtxtInferExt;
1414
use crate::traits::query::evaluate_obligation::InferCtxtExt;
@@ -567,51 +567,37 @@ fn receiver_for_self_ty<'tcx>(
567567
/// Creates the object type for the current trait. For example,
568568
/// if the current trait is `Deref`, then this will be
569569
/// `dyn Deref<Target = Self::Target> + 'static`.
570+
#[instrument(level = "trace", skip(tcx), ret)]
570571
fn object_ty_for_trait<'tcx>(
571572
tcx: TyCtxt<'tcx>,
572573
trait_def_id: DefId,
573574
lifetime: ty::Region<'tcx>,
574575
) -> Ty<'tcx> {
575-
debug!("object_ty_for_trait: trait_def_id={:?}", trait_def_id);
576-
577576
let trait_ref = ty::TraitRef::identity(tcx, trait_def_id);
577+
debug!(?trait_ref);
578578

579579
let trait_predicate = trait_ref.map_bound(|trait_ref| {
580580
ty::ExistentialPredicate::Trait(ty::ExistentialTraitRef::erase_self_ty(tcx, trait_ref))
581581
});
582+
debug!(?trait_predicate);
582583

583-
let mut associated_types = traits::supertraits(tcx, trait_ref)
584-
.flat_map(|super_trait_ref| {
585-
tcx.associated_items(super_trait_ref.def_id())
586-
.in_definition_order()
587-
.map(move |item| (super_trait_ref, item))
588-
})
589-
.filter(|(_, item)| item.kind == ty::AssocKind::Type)
590-
.collect::<Vec<_>>();
591-
592-
// existential predicates need to be in a specific order
593-
associated_types.sort_by_cached_key(|(_, item)| tcx.def_path_hash(item.def_id));
594-
595-
let projection_predicates = associated_types.into_iter().map(|(super_trait_ref, item)| {
596-
// We *can* get bound lifetimes here in cases like
597-
// `trait MyTrait: for<'s> OtherTrait<&'s T, Output=bool>`.
598-
super_trait_ref.map_bound(|super_trait_ref| {
584+
let elaborated_predicates = elaborate_trait_ref(tcx, trait_ref).filter_map(|obligation| {
585+
debug!(?obligation);
586+
let pred = obligation.predicate.to_opt_poly_projection_pred()?;
587+
Some(pred.map_bound(|p| {
599588
ty::ExistentialPredicate::Projection(ty::ExistentialProjection {
600-
term: tcx.mk_projection(item.def_id, super_trait_ref.substs).into(),
601-
item_def_id: item.def_id,
602-
substs: super_trait_ref.substs,
589+
item_def_id: p.projection_ty.item_def_id,
590+
substs: p.projection_ty.substs,
591+
term: p.term,
603592
})
604-
})
593+
}))
605594
});
606595

607596
let existential_predicates = tcx
608-
.mk_poly_existential_predicates(iter::once(trait_predicate).chain(projection_predicates));
609-
610-
let object_ty = tcx.mk_dynamic(existential_predicates, lifetime, ty::Dyn);
611-
612-
debug!("object_ty_for_trait: object_ty=`{}`", object_ty);
597+
.mk_poly_existential_predicates(iter::once(trait_predicate).chain(elaborated_predicates));
598+
debug!(?existential_predicates);
613599

614-
object_ty
600+
tcx.mk_dynamic(existential_predicates, lifetime, ty::Dyn)
615601
}
616602

617603
/// Checks the method's receiver (the `self` argument) can be dispatched on when `Self` is a

library/core/src/ptr/alignment.rs

+16
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,22 @@ impl TryFrom<usize> for Alignment {
146146
}
147147
}
148148

149+
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
150+
impl From<Alignment> for NonZeroUsize {
151+
#[inline]
152+
fn from(align: Alignment) -> NonZeroUsize {
153+
align.as_nonzero()
154+
}
155+
}
156+
157+
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
158+
impl From<Alignment> for usize {
159+
#[inline]
160+
fn from(align: Alignment) -> usize {
161+
align.as_usize()
162+
}
163+
}
164+
149165
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
150166
impl cmp::Ord for Alignment {
151167
#[inline]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# `half_open_range_patterns_in_slices`
2+
3+
The tracking issue for this feature is: [#67264]
4+
It is part of the `exclusive_range_pattern` feature,
5+
tracked at [#37854].
6+
7+
[#67264]: https://github.com/rust-lang/rust/issues/67264
8+
[#37854]: https://github.com/rust-lang/rust/issues/37854
9+
-----
10+
11+
This feature allow using top-level half-open range patterns in slices.
12+
13+
```rust
14+
#![feature(half_open_range_patterns_in_slices)]
15+
#![feature(exclusive_range_pattern)]
16+
17+
fn main() {
18+
let xs = [13, 1, 5, 2, 3, 1, 21, 8];
19+
let [a @ 3.., b @ ..3, c @ 4..6, ..] = xs else { return; };
20+
}
21+
```
22+
23+
Note that this feature is not required if the patterns are wrapped between parenthesis.
24+
25+
```rust
26+
fn main() {
27+
let xs = [13, 1];
28+
let [(a @ 3..), c] = xs else { return; };
29+
}
30+
```

src/doc/unstable-book/src/language-features/half-open-range-patterns.md

-27
This file was deleted.

src/test/ui/consts/miri_unleashed/const_refers_to_static_cross_crate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// compile-flags: -Zunleash-the-miri-inside-of-you
22
// aux-build:static_cross_crate.rs
33
// stderr-per-bitwidth
4-
#![feature(exclusive_range_pattern, half_open_range_patterns)]
4+
#![feature(exclusive_range_pattern, half_open_range_patterns_in_slices)]
55

66
extern crate static_cross_crate;
77

src/test/ui/half-open-range-patterns/exclusive_range_pattern_syntax_collision.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(half_open_range_patterns)]
1+
#![feature(half_open_range_patterns_in_slices)]
22
#![feature(exclusive_range_pattern)]
33

44
fn main() {

src/test/ui/half-open-range-patterns/exclusive_range_pattern_syntax_collision2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(half_open_range_patterns)]
1+
#![feature(half_open_range_patterns_in_slices)]
22
#![feature(exclusive_range_pattern)]
33

44
fn main() {

src/test/ui/half-open-range-patterns/exclusive_range_pattern_syntax_collision3.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![feature(half_open_range_patterns)]
21
#![feature(exclusive_range_pattern)]
32

43
fn main() {

src/test/ui/half-open-range-patterns/exclusive_range_pattern_syntax_collision3.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0308]: mismatched types
2-
--> $DIR/exclusive_range_pattern_syntax_collision3.rs:6:12
2+
--> $DIR/exclusive_range_pattern_syntax_collision3.rs:5:12
33
|
44
LL | match [5..4, 99..105, 43..44] {
55
| ----------------------- this expression has type `[std::ops::Range<{integer}>; 3]`
@@ -10,7 +10,7 @@ LL | [..9, 99..100, _] => {},
1010
found type `{integer}`
1111

1212
error[E0308]: mismatched types
13-
--> $DIR/exclusive_range_pattern_syntax_collision3.rs:6:15
13+
--> $DIR/exclusive_range_pattern_syntax_collision3.rs:5:15
1414
|
1515
LL | match [5..4, 99..105, 43..44] {
1616
| ----------------------- this expression has type `[std::ops::Range<{integer}>; 3]`
@@ -23,7 +23,7 @@ LL | [..9, 99..100, _] => {},
2323
found type `{integer}`
2424

2525
error[E0308]: mismatched types
26-
--> $DIR/exclusive_range_pattern_syntax_collision3.rs:6:19
26+
--> $DIR/exclusive_range_pattern_syntax_collision3.rs:5:19
2727
|
2828
LL | match [5..4, 99..105, 43..44] {
2929
| ----------------------- this expression has type `[std::ops::Range<{integer}>; 3]`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#![feature(exclusive_range_pattern)]
2+
3+
fn main() {
4+
let xs = [13, 1, 5, 2, 3, 1, 21, 8];
5+
let [a @ 3.., b @ ..3, c @ 4..6, ..] = xs;
6+
//~^ `X..` patterns in slices are experimental
7+
}

0 commit comments

Comments
 (0)