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 6 pull requests #102867

Merged
merged 18 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
7 changes: 5 additions & 2 deletions compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
if let PatKind::Range(Some(_), None, Spanned { .. }) = inner_pat.kind {
gate_feature_post!(
&self,
half_open_range_patterns,
half_open_range_patterns_in_slices,
pat.span,
"`X..` patterns in slices are experimental"
);
Expand Down Expand Up @@ -589,7 +589,10 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session) {
gate_all!(generators, "yield syntax is experimental");
gate_all!(raw_ref_op, "raw address of syntax is experimental");
gate_all!(const_trait_impl, "const trait impls are experimental");
gate_all!(half_open_range_patterns, "half-open range patterns are unstable");
gate_all!(
half_open_range_patterns_in_slices,
"half-open range patterns in slices are unstable"
);
gate_all!(inline_const, "inline-const is experimental");
gate_all!(inline_const_pat, "inline-const in pattern position is experimental");
gate_all!(associated_const_equality, "associated const equality is incomplete");
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_feature/src/accepted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ declare_features! (
(accepted, global_allocator, "1.28.0", Some(27389), None),
// FIXME: explain `globs`.
(accepted, globs, "1.0.0", None, None),
/// Allows using `..=X` as a pattern.
(accepted, half_open_range_patterns, "CURRENT_RUSTC_VERSION", Some(67264), None),
/// Allows using the `u128` and `i128` types.
(accepted, i128_type, "1.26.0", Some(35118), None),
/// Allows the use of `if let` expressions.
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_feature/src/active.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,8 @@ declare_features! (
(incomplete, generic_const_exprs, "1.56.0", Some(76560), None),
/// Allows using `..X`, `..=X`, `...X`, and `X..` as a pattern.
(active, half_open_range_patterns, "1.41.0", Some(67264), None),
/// Allows using `..=X` as a patterns in slices.
(active, half_open_range_patterns_in_slices, "CURRENT_RUSTC_VERSION", Some(67264), None),
/// Allows `if let` guard in match arms.
(active, if_let_guard, "1.47.0", Some(51114), None),
/// Allows using imported `main` function
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
#![feature(type_alias_impl_trait)]
#![feature(associated_type_bounds)]
#![feature(rustc_attrs)]
#![feature(half_open_range_patterns)]
#![cfg_attr(bootstrap, feature(half_open_range_patterns))]
#![feature(control_flow_enum)]
#![feature(associated_type_defaults)]
#![feature(trusted_step)]
Expand Down
36 changes: 34 additions & 2 deletions compiler/rustc_parse/src/parser/generics.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use super::{ForceCollect, Parser, TrailingToken};

use rustc_ast::token;
use rustc_ast::{self as ast, AttrVec, GenericBounds, GenericParam, GenericParamKind, WhereClause};
use rustc_ast::{
self as ast, AttrVec, GenericBounds, GenericParam, GenericParamKind, TyKind, WhereClause,
};
use rustc_errors::{Applicability, PResult};
use rustc_span::symbol::kw;

Expand Down Expand Up @@ -31,13 +33,43 @@ impl<'a> Parser<'a> {
let mut colon_span = None;
let bounds = if self.eat(&token::Colon) {
colon_span = Some(self.prev_token.span);
// recover from `impl Trait` in type param bound
if self.token.is_keyword(kw::Impl) {
let impl_span = self.token.span;
let snapshot = self.create_snapshot_for_diagnostic();
match self.parse_ty() {
Ok(p) => {
if let TyKind::ImplTrait(_, bounds) = &(*p).kind {
let span = impl_span.to(self.token.span.shrink_to_lo());
let mut err = self.struct_span_err(
span,
"expected trait bound, found `impl Trait` type",
);
err.span_label(span, "not a trait");
if let [bound, ..] = &bounds[..] {
err.span_suggestion_verbose(
impl_span.until(bound.span()),
"use the trait bounds directly",
String::new(),
Applicability::MachineApplicable,
);
}
err.emit();
return Err(err);
}
}
Err(err) => {
err.cancel();
}
}
self.restore_snapshot(snapshot);
}
self.parse_generic_bounds(colon_span)?
} else {
Vec::new()
};

let default = if self.eat(&token::Eq) { Some(self.parse_ty()?) } else { None };

Ok(GenericParam {
ident,
id: ast::DUMMY_NODE_ID,
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_parse/src/parser/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,6 @@ impl<'a> Parser<'a> {
/// expression syntax `...expr` for splatting in expressions.
fn parse_pat_range_to(&mut self, mut re: Spanned<RangeEnd>) -> PResult<'a, PatKind> {
let end = self.parse_pat_range_end()?;
self.sess.gated_spans.gate(sym::half_open_range_patterns, re.span.to(self.prev_token.span));
if let RangeEnd::Included(ref mut syn @ RangeSyntax::DotDotDot) = &mut re.node {
*syn = RangeSyntax::DotDotEq;
self.struct_span_err(re.span, "range-to patterns with `...` are not allowed")
Expand Down
33 changes: 31 additions & 2 deletions compiler/rustc_resolve/src/late/lifetimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1333,12 +1333,41 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
&& let hir::IsAsync::NotAsync = self.tcx.asyncness(lifetime_ref.hir_id.owner.def_id)
&& !self.tcx.features().anonymous_lifetime_in_impl_trait
{
rustc_session::parse::feature_err(
let mut diag = rustc_session::parse::feature_err(
&self.tcx.sess.parse_sess,
sym::anonymous_lifetime_in_impl_trait,
lifetime_ref.span,
"anonymous lifetimes in `impl Trait` are unstable",
).emit();
);

match self.tcx.hir().get_generics(lifetime_ref.hir_id.owner.def_id) {
Some(generics) => {

let new_param_sugg_tuple;

new_param_sugg_tuple = match generics.span_for_param_suggestion() {
Some(_) => {
Some((self.tcx.sess.source_map().span_through_char(generics.span, '<').shrink_to_hi(), "'a, ".to_owned()))
},
None => Some((generics.span, "<'a>".to_owned()))
};

let mut multi_sugg_vec = vec![(lifetime_ref.span.shrink_to_hi(), "'a ".to_owned())];

if let Some(new_tuple) = new_param_sugg_tuple{
multi_sugg_vec.push(new_tuple);
}

diag.span_label(lifetime_ref.span, "expected named lifetime parameter");
diag.multipart_suggestion("consider introducing a named lifetime parameter",
multi_sugg_vec,
rustc_errors::Applicability::MaybeIncorrect);

},
None => { }
}

diag.emit();
return;
}
scope = s;
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_span/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,11 @@ impl From<PathBuf> for FileName {

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

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,7 @@ symbols! {
globs,
gt,
half_open_range_patterns,
half_open_range_patterns_in_slices,
hash,
hexagon_target_feature,
hidden,
Expand Down
44 changes: 15 additions & 29 deletions compiler/rustc_trait_selection/src/traits/object_safety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
//! - not reference the erased type `Self` except for in this receiver;
//! - not have generic type parameters.

use super::elaborate_predicates;
use super::{elaborate_predicates, elaborate_trait_ref};

use crate::infer::TyCtxtInferExt;
use crate::traits::query::evaluate_obligation::InferCtxtExt;
Expand Down Expand Up @@ -567,51 +567,37 @@ fn receiver_for_self_ty<'tcx>(
/// Creates the object type for the current trait. For example,
/// if the current trait is `Deref`, then this will be
/// `dyn Deref<Target = Self::Target> + 'static`.
#[instrument(level = "trace", skip(tcx), ret)]
fn object_ty_for_trait<'tcx>(
tcx: TyCtxt<'tcx>,
trait_def_id: DefId,
lifetime: ty::Region<'tcx>,
) -> Ty<'tcx> {
debug!("object_ty_for_trait: trait_def_id={:?}", trait_def_id);

let trait_ref = ty::TraitRef::identity(tcx, trait_def_id);
debug!(?trait_ref);

let trait_predicate = trait_ref.map_bound(|trait_ref| {
ty::ExistentialPredicate::Trait(ty::ExistentialTraitRef::erase_self_ty(tcx, trait_ref))
});
debug!(?trait_predicate);

let mut associated_types = traits::supertraits(tcx, trait_ref)
.flat_map(|super_trait_ref| {
tcx.associated_items(super_trait_ref.def_id())
.in_definition_order()
.map(move |item| (super_trait_ref, item))
})
.filter(|(_, item)| item.kind == ty::AssocKind::Type)
.collect::<Vec<_>>();

// existential predicates need to be in a specific order
associated_types.sort_by_cached_key(|(_, item)| tcx.def_path_hash(item.def_id));

let projection_predicates = associated_types.into_iter().map(|(super_trait_ref, item)| {
// We *can* get bound lifetimes here in cases like
// `trait MyTrait: for<'s> OtherTrait<&'s T, Output=bool>`.
super_trait_ref.map_bound(|super_trait_ref| {
let elaborated_predicates = elaborate_trait_ref(tcx, trait_ref).filter_map(|obligation| {
debug!(?obligation);
let pred = obligation.predicate.to_opt_poly_projection_pred()?;
Some(pred.map_bound(|p| {
ty::ExistentialPredicate::Projection(ty::ExistentialProjection {
term: tcx.mk_projection(item.def_id, super_trait_ref.substs).into(),
item_def_id: item.def_id,
substs: super_trait_ref.substs,
item_def_id: p.projection_ty.item_def_id,
substs: p.projection_ty.substs,
term: p.term,
})
})
}))
});

let existential_predicates = tcx
.mk_poly_existential_predicates(iter::once(trait_predicate).chain(projection_predicates));

let object_ty = tcx.mk_dynamic(existential_predicates, lifetime, ty::Dyn);

debug!("object_ty_for_trait: object_ty=`{}`", object_ty);
.mk_poly_existential_predicates(iter::once(trait_predicate).chain(elaborated_predicates));
debug!(?existential_predicates);

object_ty
tcx.mk_dynamic(existential_predicates, lifetime, ty::Dyn)
}

/// Checks the method's receiver (the `self` argument) can be dispatched on when `Self` is a
Expand Down
16 changes: 16 additions & 0 deletions library/core/src/ptr/alignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,22 @@ impl TryFrom<usize> for Alignment {
}
}

#[unstable(feature = "ptr_alignment_type", issue = "102070")]
impl From<Alignment> for NonZeroUsize {
#[inline]
fn from(align: Alignment) -> NonZeroUsize {
align.as_nonzero()
}
}

#[unstable(feature = "ptr_alignment_type", issue = "102070")]
impl From<Alignment> for usize {
#[inline]
fn from(align: Alignment) -> usize {
align.as_usize()
}
}

#[unstable(feature = "ptr_alignment_type", issue = "102070")]
impl cmp::Ord for Alignment {
#[inline]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# `half_open_range_patterns_in_slices`

The tracking issue for this feature is: [#67264]
It is part of the `exclusive_range_pattern` feature,
tracked at [#37854].

[#67264]: https://github.com/rust-lang/rust/issues/67264
[#37854]: https://github.com/rust-lang/rust/issues/37854
-----

This feature allow using top-level half-open range patterns in slices.

```rust
#![feature(half_open_range_patterns_in_slices)]
#![feature(exclusive_range_pattern)]

fn main() {
let xs = [13, 1, 5, 2, 3, 1, 21, 8];
let [a @ 3.., b @ ..3, c @ 4..6, ..] = xs else { return; };
}
```

Note that this feature is not required if the patterns are wrapped between parenthesis.

```rust
fn main() {
let xs = [13, 1];
let [(a @ 3..), c] = xs else { return; };
}
```

This file was deleted.

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

extern crate static_cross_crate;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(half_open_range_patterns)]
#![feature(half_open_range_patterns_in_slices)]
#![feature(exclusive_range_pattern)]

fn main() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(half_open_range_patterns)]
#![feature(half_open_range_patterns_in_slices)]
#![feature(exclusive_range_pattern)]

fn main() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![feature(half_open_range_patterns)]
#![feature(exclusive_range_pattern)]

fn main() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0308]: mismatched types
--> $DIR/exclusive_range_pattern_syntax_collision3.rs:6:12
--> $DIR/exclusive_range_pattern_syntax_collision3.rs:5:12
|
LL | match [5..4, 99..105, 43..44] {
| ----------------------- this expression has type `[std::ops::Range<{integer}>; 3]`
Expand All @@ -10,7 +10,7 @@ LL | [..9, 99..100, _] => {},
found type `{integer}`

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

error[E0308]: mismatched types
--> $DIR/exclusive_range_pattern_syntax_collision3.rs:6:19
--> $DIR/exclusive_range_pattern_syntax_collision3.rs:5:19
|
LL | match [5..4, 99..105, 43..44] {
| ----------------------- this expression has type `[std::ops::Range<{integer}>; 3]`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#![feature(exclusive_range_pattern)]

fn main() {
let xs = [13, 1, 5, 2, 3, 1, 21, 8];
let [a @ 3.., b @ ..3, c @ 4..6, ..] = xs;
//~^ `X..` patterns in slices are experimental
}
Loading