Skip to content

Commit

Permalink
Auto merge of rust-lang#88527 - m-ou-se:rollup-az6xtc5, r=m-ou-se
Browse files Browse the repository at this point in the history
Rollup of 14 pull requests

Successful merges:

 - rust-lang#88394 (Document `std::env::current_exe` possible rename behaviour)
 - rust-lang#88406 (Tait nest infer test)
 - rust-lang#88408 (Add inference cycle TAIT test)
 - rust-lang#88409 (Add auto trait leakage TAIT test)
 - rust-lang#88413 (Add weird return types TAIT test)
 - rust-lang#88450 (fix(rustc_parse): correct span in `maybe_whole_expr!`)
 - rust-lang#88462 (rustdoc: Stop using resolver for macro loading)
 - rust-lang#88465 (Adding examples to docs of `std::time` module)
 - rust-lang#88486 (Remove unused arena macro args)
 - rust-lang#88492 (Use MaybeUninit::write in functor.rs)
 - rust-lang#88496 (Fix prelude collision lint suggestion for generics with lifetimes)
 - rust-lang#88497 (Fix prelude collision suggestions for glob imported traits. )
 - rust-lang#88503 (Warn when [T; N].into_iter() is ambiguous in the new edition. )
 - rust-lang#88509 (Don't suggest extra <> in dyn suggestion.)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
  • Loading branch information
bors committed Aug 31, 2021
2 parents fe37929 + feafda8 commit 76d18cf
Show file tree
Hide file tree
Showing 46 changed files with 698 additions and 89 deletions.
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4405,6 +4405,7 @@ dependencies = [
"rustc_hir_pretty",
"rustc_index",
"rustc_infer",
"rustc_lint",
"rustc_macros",
"rustc_middle",
"rustc_session",
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_arena/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ pub macro which_arena_for_type {
}

#[rustc_macro_transparency = "semitransparent"]
pub macro declare_arena([], [$($a:tt $name:ident: $ty:ty,)*], $tcx:lifetime) {
pub macro declare_arena([$($a:tt $name:ident: $ty:ty,)*], $tcx:lifetime) {
#[derive(Default)]
pub struct Arena<$tcx> {
pub dropless: $crate::DroplessArena,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ mod path;

const HIR_ID_COUNTER_LOCKED: u32 = 0xFFFFFFFF;

rustc_hir::arena_types!(rustc_arena::declare_arena, [], 'tcx);
rustc_hir::arena_types!(rustc_arena::declare_arena, 'tcx);

struct LoweringContext<'a, 'hir: 'a> {
/// Used to assign IDs to HIR nodes that do not directly correspond to AST nodes.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_data_structures/src/functor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl<T> IdFunctor for Box<T> {
// inverse of `Box::assume_init()` and should be safe.
let mut raw: Box<mem::MaybeUninit<T>> = Box::from_raw(raw.cast());
// SAFETY: Write the mapped value back into the `Box`.
ptr::write(raw.as_mut_ptr(), f(value));
raw.write(f(value));
// SAFETY: We just initialized `raw`.
raw.assume_init()
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_hir/src/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
/// where `T` is the type listed. These impls will appear in the implement_ty_decoder! macro.
#[macro_export]
macro_rules! arena_types {
($macro:path, $args:tt, $tcx:lifetime) => (
$macro!($args, [
($macro:path, $tcx:lifetime) => (
$macro!([
// HIR types
[few] hir_krate: rustc_hir::Crate<$tcx>,
[] arm: rustc_hir::Arm<$tcx>,
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ mod traits;
mod types;
mod unused;

pub use array_into_iter::ARRAY_INTO_ITER;

use rustc_ast as ast;
use rustc_hir as hir;
use rustc_hir::def_id::LocalDefId;
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_middle/src/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
/// listed. These impls will appear in the implement_ty_decoder! macro.
#[macro_export]
macro_rules! arena_types {
($macro:path, $args:tt, $tcx:lifetime) => (
$macro!($args, [
($macro:path, $tcx:lifetime) => (
$macro!([
[] layouts: rustc_target::abi::Layout,
// AdtDef are interned and compared by address
[] adt_def: rustc_middle::ty::AdtDef,
Expand Down Expand Up @@ -109,4 +109,4 @@ macro_rules! arena_types {
)
}

arena_types!(rustc_arena::declare_arena, [], 'tcx);
arena_types!(rustc_arena::declare_arena, 'tcx);
6 changes: 3 additions & 3 deletions compiler/rustc_middle/src/ty/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,15 +437,15 @@ macro_rules! impl_arena_allocatable_decoder {
}

macro_rules! impl_arena_allocatable_decoders {
([], [$($a:tt $name:ident: $ty:ty,)*], $tcx:lifetime) => {
([$($a:tt $name:ident: $ty:ty,)*], $tcx:lifetime) => {
$(
impl_arena_allocatable_decoder!($a [[$name: $ty], $tcx]);
)*
}
}

rustc_hir::arena_types!(impl_arena_allocatable_decoders, [], 'tcx);
arena_types!(impl_arena_allocatable_decoders, [], 'tcx);
rustc_hir::arena_types!(impl_arena_allocatable_decoders, 'tcx);
arena_types!(impl_arena_allocatable_decoders, 'tcx);

#[macro_export]
macro_rules! implement_ty_decoder {
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ macro_rules! maybe_whole_expr {
let path = path.clone();
$p.bump();
return Ok($p.mk_expr(
$p.token.span,
$p.prev_token.span,
ExprKind::Path(None, path),
AttrVec::new(),
));
Expand All @@ -50,7 +50,7 @@ macro_rules! maybe_whole_expr {
let block = block.clone();
$p.bump();
return Ok($p.mk_expr(
$p.token.span,
$p.prev_token.span,
ExprKind::Block(block, None),
AttrVec::new(),
));
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_typeck/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ rustc_index = { path = "../rustc_index" }
rustc_infer = { path = "../rustc_infer" }
rustc_trait_selection = { path = "../rustc_trait_selection" }
rustc_ty_utils = { path = "../rustc_ty_utils" }
rustc_lint = { path = "../rustc_lint" }
15 changes: 10 additions & 5 deletions compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
});

if result.is_ok() {
self.maybe_lint_bare_trait(qpath, hir_id);
self.maybe_lint_bare_trait(qpath, hir_id, span);
self.register_wf_obligation(ty.into(), qself.span, traits::WellFormed(None));
}

Expand All @@ -927,18 +927,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
)
}

fn maybe_lint_bare_trait(&self, qpath: &QPath<'_>, hir_id: hir::HirId) {
fn maybe_lint_bare_trait(&self, qpath: &QPath<'_>, hir_id: hir::HirId, span: Span) {
if let QPath::TypeRelative(self_ty, _) = qpath {
if let TyKind::TraitObject([poly_trait_ref, ..], _, TraitObjectSyntax::None) =
self_ty.kind
{
let msg = "trait objects without an explicit `dyn` are deprecated";
let (sugg, app) = match self.tcx.sess.source_map().span_to_snippet(self_ty.span) {
Ok(s) if poly_trait_ref.trait_ref.path.is_global() => {
(format!("<dyn ({})>", s), Applicability::MachineApplicable)
(format!("dyn ({})", s), Applicability::MachineApplicable)
}
Ok(s) => (format!("<dyn {}>", s), Applicability::MachineApplicable),
Err(_) => ("<dyn <type>>".to_string(), Applicability::HasPlaceholders),
Ok(s) => (format!("dyn {}", s), Applicability::MachineApplicable),
Err(_) => ("dyn <type>".to_string(), Applicability::HasPlaceholders),
};
// Wrap in `<..>` if it isn't already.
let sugg = match self.tcx.sess.source_map().span_to_snippet(span) {
Ok(s) if s.starts_with('<') => sugg,
_ => format!("<{}>", sugg),
};
let replace = String::from("use `dyn`");
if self.sess().edition() >= Edition::Edition2021 {
Expand Down
98 changes: 62 additions & 36 deletions compiler/rustc_typeck/src/check/method/prelude2021.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use rustc_ast::Mutability;
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_middle::ty::subst::InternalSubsts;
use rustc_middle::ty::{Adt, Ref, Ty};
use rustc_middle::ty::{Adt, Array, Ref, Ty};
use rustc_session::lint::builtin::RUST_2021_PRELUDE_COLLISIONS;
use rustc_span::symbol::kw::Underscore;
use rustc_span::symbol::kw::{Empty, Underscore};
use rustc_span::symbol::{sym, Ident};
use rustc_span::Span;
use rustc_trait_selection::infer::InferCtxtExt;
Expand Down Expand Up @@ -38,10 +38,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
return;
}

// These are the method names that were added to prelude in Rust 2021
if !matches!(segment.ident.name, sym::try_into) {
return;
}
let prelude_or_array_lint = match segment.ident.name {
// `try_into` was added to the prelude in Rust 2021.
sym::try_into => RUST_2021_PRELUDE_COLLISIONS,
// `into_iter` wasn't added to the prelude,
// but `[T; N].into_iter()` doesn't resolve to IntoIterator::into_iter
// before Rust 2021, which results in the same problem.
// It is only a problem for arrays.
sym::into_iter if let Array(..) = self_ty.kind() => {
// In this case, it wasn't really a prelude addition that was the problem.
// Instead, the problem is that the array-into_iter hack will no longer apply in Rust 2021.
rustc_lint::ARRAY_INTO_ITER
}
_ => return,
};

// No need to lint if method came from std/core, as that will now be in the prelude
if matches!(self.tcx.crate_name(pick.item.def_id.krate), sym::std | sym::core) {
Expand Down Expand Up @@ -69,7 +79,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// Inherent impls only require not relying on autoref and autoderef in order to
// ensure that the trait implementation won't be used
self.tcx.struct_span_lint_hir(
RUST_2021_PRELUDE_COLLISIONS,
prelude_or_array_lint,
self_expr.hir_id,
self_expr.span,
|lint| {
Expand Down Expand Up @@ -130,7 +140,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// trait implementations require full disambiguation to not clash with the new prelude
// additions (i.e. convert from dot-call to fully-qualified call)
self.tcx.struct_span_lint_hir(
RUST_2021_PRELUDE_COLLISIONS,
prelude_or_array_lint,
call_expr.hir_id,
call_expr.span,
|lint| {
Expand Down Expand Up @@ -239,47 +249,58 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let trait_path = self.trait_path_or_bare_name(span, expr_id, pick.item.container.id());
let trait_generics = self.tcx.generics_of(pick.item.container.id());

let parameter_count = trait_generics.count() - (trait_generics.has_self as usize);
let trait_name = if parameter_count == 0 {
trait_path
} else {
format!(
"{}<{}>",
trait_path,
std::iter::repeat("_").take(parameter_count).collect::<Vec<_>>().join(", ")
)
};
let trait_name =
if trait_generics.params.len() <= trait_generics.has_self as usize {
trait_path
} else {
let counts = trait_generics.own_counts();
format!(
"{}<{}>",
trait_path,
std::iter::repeat("'_")
.take(counts.lifetimes)
.chain(std::iter::repeat("_").take(
counts.types + counts.consts - trait_generics.has_self as usize
))
.collect::<Vec<_>>()
.join(", ")
)
};

let mut lint = lint.build(&format!(
"trait-associated function `{}` will become ambiguous in Rust 2021",
method_name.name
));

let self_ty_name = self
let mut self_ty_name = self
.sess()
.source_map()
.span_to_snippet(self_ty_span)
.unwrap_or_else(|_| self_ty.to_string());

let self_ty_generics_count = match self_ty.kind() {
// Get the number of generics the self type has (if an Adt) unless we can determine that
// the user has written the self type with generics already which we (naively) do by looking
// for a "<" in `self_ty_name`.
Adt(def, _) if !self_ty_name.contains('<') => self.tcx.generics_of(def.did).count(),
_ => 0,
};
let self_ty_generics = if self_ty_generics_count > 0 {
format!("<{}>", vec!["_"; self_ty_generics_count].join(", "))
} else {
String::new()
};
// Get the number of generics the self type has (if an Adt) unless we can determine that
// the user has written the self type with generics already which we (naively) do by looking
// for a "<" in `self_ty_name`.
if !self_ty_name.contains('<') {
if let Adt(def, _) = self_ty.kind() {
let generics = self.tcx.generics_of(def.did);
if !generics.params.is_empty() {
let counts = generics.own_counts();
self_ty_name += &format!(
"<{}>",
std::iter::repeat("'_")
.take(counts.lifetimes)
.chain(std::iter::repeat("_").take(counts.types + counts.consts))
.collect::<Vec<_>>()
.join(", ")
);
}
}
}
lint.span_suggestion(
span,
"disambiguate the associated function",
format!(
"<{}{} as {}>::{}",
self_ty_name, self_ty_generics, trait_name, method_name.name,
),
format!("<{} as {}>::{}", self_ty_name, trait_name, method_name.name,),
Applicability::MachineApplicable,
);

Expand Down Expand Up @@ -322,7 +343,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.filter_map(|item| if item.ident.name != Underscore { Some(item.ident) } else { None })
.next();
if let Some(any_id) = any_id {
return Some(format!("{}", any_id));
if any_id.name == Empty {
// Glob import, so just use its name.
return None;
} else {
return Some(format!("{}", any_id));
}
}

// All that is left is `_`! We need to use the full path. It doesn't matter which one we pick,
Expand Down
17 changes: 12 additions & 5 deletions library/core/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@

//! Temporal quantification.
//!
//! Example:
//! # Examples:
//!
//! There are multiple ways to create a new [`Duration`]:
//!
//! ```
//! use std::time::Duration;
//! # use std::time::Duration;
//! let five_seconds = Duration::from_secs(5);
//! assert_eq!(five_seconds, Duration::from_millis(5_000));
//! assert_eq!(five_seconds, Duration::from_micros(5_000_000));
//! assert_eq!(five_seconds, Duration::from_nanos(5_000_000_000));
//!
//! let five_seconds = Duration::new(5, 0);
//! // both declarations are equivalent
//! assert_eq!(Duration::new(5, 0), Duration::from_secs(5));
//! let ten_seconds = Duration::from_secs(10);
//! let seven_nanos = Duration::from_nanos(7);
//! let total = ten_seconds + seven_nanos;
//! assert_eq!(total, Duration::new(10, 7));
//! ```

use crate::fmt;
Expand Down
3 changes: 3 additions & 0 deletions library/std/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,9 @@ pub fn temp_dir() -> PathBuf {
/// return the path of the symbolic link and other platforms will return the
/// path of the symbolic link’s target.
///
/// If the executable is renamed while it is running, platforms may return the
/// path at the time it was loaded instead of the new path.
///
/// # Errors
///
/// Acquiring the path of the current executable is a platform-specific operation
Expand Down
31 changes: 25 additions & 6 deletions library/std/src/time.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
//! Temporal quantification.
//!
//! Example:
//! # Examples:
//!
//! There are multiple ways to create a new [`Duration`]:
//!
//! ```
//! # use std::time::Duration;
//! let five_seconds = Duration::from_secs(5);
//! assert_eq!(five_seconds, Duration::from_millis(5_000));
//! assert_eq!(five_seconds, Duration::from_micros(5_000_000));
//! assert_eq!(five_seconds, Duration::from_nanos(5_000_000_000));
//!
//! let ten_seconds = Duration::from_secs(10);
//! let seven_nanos = Duration::from_nanos(7);
//! let total = ten_seconds + seven_nanos;
//! assert_eq!(total, Duration::new(10, 7));
//! ```
//! use std::time::Duration;
//!
//! let five_seconds = Duration::new(5, 0);
//! // both declarations are equivalent
//! assert_eq!(Duration::new(5, 0), Duration::from_secs(5));
//! Using [`Instant`] to calculate how long a function took to run:
//!
//! ```ignore (incomplete)
//! let now = Instant::now();
//!
//! // Calling a slow function, it may take a while
//! slow_function();
//!
//! let elapsed_time = now.elapsed();
//! println!("Running slow_function() took {} seconds.", elapsed_time.as_secs());
//! ```

#![stable(feature = "time", since = "1.3.0")]
Expand All @@ -26,7 +45,7 @@ use crate::sys_common::FromInner;
pub use core::time::Duration;

/// A measurement of a monotonically nondecreasing clock.
/// Opaque and useful only with `Duration`.
/// Opaque and useful only with [`Duration`].
///
/// Instants are always guaranteed to be no less than any previously measured
/// instant when created, and are often useful for tasks such as measuring
Expand Down
Loading

0 comments on commit 76d18cf

Please sign in to comment.