Skip to content

Commit d4c3643

Browse files
committed
Auto merge of rust-lang#96904 - JohnTitor:rollup-f1sz5x0, r=JohnTitor
Rollup of 6 pull requests Successful merges: - rust-lang#96717 (Handle mismatched generic param kinds in trait impls betterly) - rust-lang#96725 (Expose process windows_process_extensions_main_thread_handle on Windows) - rust-lang#96849 (Move some tests to more reasonable places) - rust-lang#96861 (Use Rust 2021 prelude in std itself.) - rust-lang#96879 (rustdoc: search result ranking fix) - rust-lang#96882 (Don't subst an AdtDef with its own substs) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents eead58e + c5f2c44 commit d4c3643

Some content is hidden

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

68 files changed

+368
-126
lines changed

compiler/rustc_lint/src/builtin.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use rustc_index::vec::Idx;
4141
use rustc_middle::lint::{in_external_macro, LintDiagnosticBuilder};
4242
use rustc_middle::ty::layout::{LayoutError, LayoutOf};
4343
use rustc_middle::ty::print::with_no_trimmed_paths;
44-
use rustc_middle::ty::subst::{GenericArgKind, Subst};
44+
use rustc_middle::ty::subst::GenericArgKind;
4545
use rustc_middle::ty::Instance;
4646
use rustc_middle::ty::{self, Ty, TyCtxt};
4747
use rustc_session::lint::{BuiltinLintDiagnostics, FutureIncompatibilityReason};
@@ -2777,7 +2777,7 @@ impl ClashingExternDeclarations {
27772777
let mut ty = ty;
27782778
loop {
27792779
if let ty::Adt(def, substs) = *ty.kind() {
2780-
let is_transparent = def.subst(tcx, substs).repr().transparent();
2780+
let is_transparent = def.repr().transparent();
27812781
let is_non_null = crate::types::nonnull_optimization_guaranteed(tcx, def);
27822782
debug!(
27832783
"non_transparent_ty({:?}) -- type is transparent? {}, type is non-null? {}",
@@ -2837,11 +2837,7 @@ impl ClashingExternDeclarations {
28372837

28382838
ensure_sufficient_stack(|| {
28392839
match (a_kind, b_kind) {
2840-
(Adt(a_def, a_substs), Adt(b_def, b_substs)) => {
2841-
let a = a.subst(cx.tcx, a_substs);
2842-
let b = b.subst(cx.tcx, b_substs);
2843-
debug!("Comparing {:?} and {:?}", a, b);
2844-
2840+
(Adt(a_def, _), Adt(b_def, _)) => {
28452841
// We can immediately rule out these types as structurally same if
28462842
// their layouts differ.
28472843
match compare_layouts(a, b) {

compiler/rustc_typeck/src/check/compare_method.rs

+116-51
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ use rustc_hir::intravisit;
77
use rustc_hir::{GenericParamKind, ImplItemKind, TraitItemKind};
88
use rustc_infer::infer::{self, InferOk, TyCtxtInferExt};
99
use rustc_infer::traits::util;
10-
use rustc_middle::ty;
1110
use rustc_middle::ty::error::{ExpectedFound, TypeError};
1211
use rustc_middle::ty::subst::{InternalSubsts, Subst};
1312
use rustc_middle::ty::util::ExplicitSelf;
13+
use rustc_middle::ty::{self, DefIdTree};
1414
use rustc_middle::ty::{GenericParamDefKind, ToPredicate, TyCtxt};
1515
use rustc_span::Span;
1616
use rustc_trait_selection::traits::error_reporting::InferCtxtExt;
@@ -48,6 +48,10 @@ crate fn compare_impl_method<'tcx>(
4848
return;
4949
}
5050

51+
if let Err(_) = compare_generic_param_kinds(tcx, impl_m, trait_m) {
52+
return;
53+
}
54+
5155
if let Err(_) =
5256
compare_number_of_method_arguments(tcx, impl_m, impl_m_span, trait_m, trait_item_span)
5357
{
@@ -62,10 +66,6 @@ crate fn compare_impl_method<'tcx>(
6266
{
6367
return;
6468
}
65-
66-
if let Err(_) = compare_const_param_types(tcx, impl_m, trait_m, trait_item_span) {
67-
return;
68-
}
6969
}
7070

7171
fn compare_predicate_entailment<'tcx>(
@@ -579,6 +579,27 @@ fn compare_self_type<'tcx>(
579579
Ok(())
580580
}
581581

582+
/// Checks that the number of generics on a given assoc item in a trait impl is the same
583+
/// as the number of generics on the respective assoc item in the trait definition.
584+
///
585+
/// For example this code emits the errors in the following code:
586+
/// ```
587+
/// trait Trait {
588+
/// fn foo();
589+
/// type Assoc<T>;
590+
/// }
591+
///
592+
/// impl Trait for () {
593+
/// fn foo<T>() {}
594+
/// //~^ error
595+
/// type Assoc = u32;
596+
/// //~^ error
597+
/// }
598+
/// ```
599+
///
600+
/// Notably this does not error on `foo<T>` implemented as `foo<const N: u8>` or
601+
/// `foo<const N: u8>` implemented as `foo<const N: u32>`. This is handled in
602+
/// [`compare_generic_param_kinds`]. This function also does not handle lifetime parameters
582603
fn compare_number_of_generics<'tcx>(
583604
tcx: TyCtxt<'tcx>,
584605
impl_: &ty::AssocItem,
@@ -589,6 +610,15 @@ fn compare_number_of_generics<'tcx>(
589610
let trait_own_counts = tcx.generics_of(trait_.def_id).own_counts();
590611
let impl_own_counts = tcx.generics_of(impl_.def_id).own_counts();
591612

613+
// This avoids us erroring on `foo<T>` implemented as `foo<const N: u8>` as this is implemented
614+
// in `compare_generic_param_kinds` which will give a nicer error message than something like:
615+
// "expected 1 type parameter, found 0 type parameters"
616+
if (trait_own_counts.types + trait_own_counts.consts)
617+
== (impl_own_counts.types + impl_own_counts.consts)
618+
{
619+
return Ok(());
620+
}
621+
592622
let matchings = [
593623
("type", trait_own_counts.types, impl_own_counts.types),
594624
("const", trait_own_counts.consts, impl_own_counts.consts),
@@ -914,60 +944,93 @@ fn compare_synthetic_generics<'tcx>(
914944
if let Some(reported) = error_found { Err(reported) } else { Ok(()) }
915945
}
916946

917-
fn compare_const_param_types<'tcx>(
947+
/// Checks that all parameters in the generics of a given assoc item in a trait impl have
948+
/// the same kind as the respective generic parameter in the trait def.
949+
///
950+
/// For example all 4 errors in the following code are emitted here:
951+
/// ```
952+
/// trait Foo {
953+
/// fn foo<const N: u8>();
954+
/// type bar<const N: u8>;
955+
/// fn baz<const N: u32>();
956+
/// type blah<T>;
957+
/// }
958+
///
959+
/// impl Foo for () {
960+
/// fn foo<const N: u64>() {}
961+
/// //~^ error
962+
/// type bar<const N: u64> {}
963+
/// //~^ error
964+
/// fn baz<T>() {}
965+
/// //~^ error
966+
/// type blah<const N: i64> = u32;
967+
/// //~^ error
968+
/// }
969+
/// ```
970+
///
971+
/// This function does not handle lifetime parameters
972+
fn compare_generic_param_kinds<'tcx>(
918973
tcx: TyCtxt<'tcx>,
919-
impl_m: &ty::AssocItem,
920-
trait_m: &ty::AssocItem,
921-
trait_item_span: Option<Span>,
974+
impl_item: &ty::AssocItem,
975+
trait_item: &ty::AssocItem,
922976
) -> Result<(), ErrorGuaranteed> {
923-
let const_params_of = |def_id| {
924-
tcx.generics_of(def_id).params.iter().filter_map(|param| match param.kind {
925-
GenericParamDefKind::Const { .. } => Some(param.def_id),
926-
_ => None,
977+
assert_eq!(impl_item.kind, trait_item.kind);
978+
979+
let ty_const_params_of = |def_id| {
980+
tcx.generics_of(def_id).params.iter().filter(|param| {
981+
matches!(
982+
param.kind,
983+
GenericParamDefKind::Const { .. } | GenericParamDefKind::Type { .. }
984+
)
927985
})
928986
};
929-
let const_params_impl = const_params_of(impl_m.def_id);
930-
let const_params_trait = const_params_of(trait_m.def_id);
931-
932-
for (const_param_impl, const_param_trait) in iter::zip(const_params_impl, const_params_trait) {
933-
let impl_ty = tcx.type_of(const_param_impl);
934-
let trait_ty = tcx.type_of(const_param_trait);
935-
if impl_ty != trait_ty {
936-
let (impl_span, impl_ident) = match tcx.hir().get_if_local(const_param_impl) {
937-
Some(hir::Node::GenericParam(hir::GenericParam { span, name, .. })) => (
938-
span,
939-
match name {
940-
hir::ParamName::Plain(ident) => Some(ident),
941-
_ => None,
942-
},
943-
),
944-
other => bug!(
945-
"expected GenericParam, found {:?}",
946-
other.map_or_else(|| "nothing".to_string(), |n| format!("{:?}", n))
947-
),
948-
};
949-
let trait_span = match tcx.hir().get_if_local(const_param_trait) {
950-
Some(hir::Node::GenericParam(hir::GenericParam { span, .. })) => Some(span),
951-
_ => None,
952-
};
987+
988+
for (param_impl, param_trait) in
989+
iter::zip(ty_const_params_of(impl_item.def_id), ty_const_params_of(trait_item.def_id))
990+
{
991+
use GenericParamDefKind::*;
992+
if match (&param_impl.kind, &param_trait.kind) {
993+
(Const { .. }, Const { .. })
994+
if tcx.type_of(param_impl.def_id) != tcx.type_of(param_trait.def_id) =>
995+
{
996+
true
997+
}
998+
(Const { .. }, Type { .. }) | (Type { .. }, Const { .. }) => true,
999+
// this is exhaustive so that anyone adding new generic param kinds knows
1000+
// to make sure this error is reported for them.
1001+
(Const { .. }, Const { .. }) | (Type { .. }, Type { .. }) => false,
1002+
(Lifetime { .. }, _) | (_, Lifetime { .. }) => unreachable!(),
1003+
} {
1004+
let param_impl_span = tcx.def_span(param_impl.def_id);
1005+
let param_trait_span = tcx.def_span(param_trait.def_id);
1006+
9531007
let mut err = struct_span_err!(
9541008
tcx.sess,
955-
*impl_span,
1009+
param_impl_span,
9561010
E0053,
957-
"method `{}` has an incompatible const parameter type for trait",
958-
trait_m.name
959-
);
960-
err.span_note(
961-
trait_span.map_or_else(|| trait_item_span.unwrap_or(*impl_span), |span| *span),
962-
&format!(
963-
"the const parameter{} has type `{}`, but the declaration \
964-
in trait `{}` has type `{}`",
965-
&impl_ident.map_or_else(|| "".to_string(), |ident| format!(" `{ident}`")),
966-
impl_ty,
967-
tcx.def_path_str(trait_m.def_id),
968-
trait_ty
969-
),
1011+
"{} `{}` has an incompatible generic parameter for trait `{}`",
1012+
assoc_item_kind_str(&impl_item),
1013+
trait_item.name,
1014+
&tcx.def_path_str(tcx.parent(trait_item.def_id))
9701015
);
1016+
1017+
let make_param_message = |prefix: &str, param: &ty::GenericParamDef| match param.kind {
1018+
Const { .. } => {
1019+
format!("{} const parameter of type `{}`", prefix, tcx.type_of(param.def_id))
1020+
}
1021+
Type { .. } => format!("{} type parameter", prefix),
1022+
Lifetime { .. } => unreachable!(),
1023+
};
1024+
1025+
let trait_header_span = tcx.def_ident_span(tcx.parent(trait_item.def_id)).unwrap();
1026+
err.span_label(trait_header_span, "");
1027+
err.span_label(param_trait_span, make_param_message("expected", param_trait));
1028+
1029+
let impl_header_span =
1030+
tcx.sess.source_map().guess_head_span(tcx.def_span(tcx.parent(impl_item.def_id)));
1031+
err.span_label(impl_header_span, "");
1032+
err.span_label(param_impl_span, make_param_message("found", param_impl));
1033+
9711034
let reported = err.emit();
9721035
return Err(reported);
9731036
}
@@ -1095,6 +1158,8 @@ crate fn compare_ty_impl<'tcx>(
10951158
let _: Result<(), ErrorGuaranteed> = (|| {
10961159
compare_number_of_generics(tcx, impl_ty, impl_ty_span, trait_ty, trait_item_span)?;
10971160

1161+
compare_generic_param_kinds(tcx, impl_ty, trait_ty)?;
1162+
10981163
let sp = tcx.def_span(impl_ty.def_id);
10991164
compare_type_predicate_entailment(tcx, impl_ty, sp, trait_ty, impl_trait_ref)?;
11001165

library/std/src/collections/hash/map.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::collections::TryReserveErrorKind;
1212
use crate::fmt::{self, Debug};
1313
#[allow(deprecated)]
1414
use crate::hash::{BuildHasher, Hash, Hasher, SipHasher13};
15-
use crate::iter::{FromIterator, FusedIterator};
15+
use crate::iter::FusedIterator;
1616
use crate::ops::Index;
1717
use crate::sys;
1818

library/std/src/collections/hash/set.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::borrow::Borrow;
77
use crate::collections::TryReserveError;
88
use crate::fmt;
99
use crate::hash::{BuildHasher, Hash};
10-
use crate::iter::{Chain, FromIterator, FusedIterator};
10+
use crate::iter::{Chain, FusedIterator};
1111
use crate::ops::{BitAnd, BitOr, BitXor, Sub};
1212

1313
use super::map::{map_try_reserve_error, RandomState};

library/std/src/ffi/os_str.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::cmp;
66
use crate::collections::TryReserveError;
77
use crate::fmt;
88
use crate::hash::{Hash, Hasher};
9-
use crate::iter::{Extend, FromIterator};
9+
use crate::iter::Extend;
1010
use crate::ops;
1111
use crate::rc::Rc;
1212
use crate::str::FromStr;

library/std/src/io/cursor.rs

-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ use crate::alloc::Allocator;
77
use crate::cmp;
88
use crate::io::{self, ErrorKind, IoSlice, IoSliceMut, ReadBuf, SeekFrom};
99

10-
use core::convert::TryInto;
11-
1210
/// A `Cursor` wraps an in-memory buffer and provides it with a
1311
/// [`Seek`] implementation.
1412
///

library/std/src/io/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,6 @@
252252
mod tests;
253253

254254
use crate::cmp;
255-
use crate::convert::TryInto;
256255
use crate::fmt;
257256
use crate::mem::replace;
258257
use crate::ops::{Deref, DerefMut};

library/std/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@
343343
// to import the prelude implicitly when building crates that depend on std.
344344
#[prelude_import]
345345
#[allow(unused)]
346-
use prelude::v1::*;
346+
use prelude::rust_2021::*;
347347

348348
// Access to Bencher, etc.
349349
#[cfg(test)]

library/std/src/net/addr.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
mod tests;
33

44
use crate::cmp::Ordering;
5-
use crate::convert::TryInto;
65
use crate::fmt;
76
use crate::hash;
87
use crate::io::{self, Write};

library/std/src/net/parser.rs

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#[cfg(test)]
77
mod tests;
88

9-
use crate::convert::TryInto as _;
109
use crate::error::Error;
1110
use crate::fmt;
1211
use crate::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};

library/std/src/os/unix/net/ancillary.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use super::{sockaddr_un, SocketAddr};
2-
use crate::convert::TryFrom;
32
use crate::io::{self, IoSlice, IoSliceMut};
43
use crate::marker::PhantomData;
54
use crate::mem::{size_of, zeroed};

library/std/src/os/unix/net/tests.rs

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use crate::io::{self, ErrorKind, IoSlice, IoSliceMut};
1010
target_os = "netbsd",
1111
target_os = "openbsd",
1212
))]
13-
use crate::iter::FromIterator;
1413
#[cfg(any(
1514
target_os = "android",
1615
target_os = "dragonfly",

library/std/src/os/windows/io/handle.rs

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#![unstable(feature = "io_safety", issue = "87074")]
44

55
use super::raw::{AsRawHandle, FromRawHandle, IntoRawHandle, RawHandle};
6-
use crate::convert::TryFrom;
76
use crate::fmt;
87
use crate::fs;
98
use crate::io;

library/std/src/os/windows/process.rs

+14
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,17 @@ impl CommandExt for process::Command {
180180
self
181181
}
182182
}
183+
184+
#[unstable(feature = "windows_process_extensions_main_thread_handle", issue = "96723")]
185+
pub trait ChildExt: Sealed {
186+
/// Extracts the main thread raw handle, without taking ownership
187+
#[unstable(feature = "windows_process_extensions_main_thread_handle", issue = "96723")]
188+
fn main_thread_handle(&self) -> BorrowedHandle<'_>;
189+
}
190+
191+
#[unstable(feature = "windows_process_extensions_main_thread_handle", issue = "96723")]
192+
impl ChildExt for process::Child {
193+
fn main_thread_handle(&self) -> BorrowedHandle<'_> {
194+
self.handle.main_thread_handle()
195+
}
196+
}

library/std/src/sys/hermit/net.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::convert::TryFrom;
21
use crate::fmt;
32
use crate::io::{self, ErrorKind, IoSlice, IoSliceMut};
43
use crate::net::{IpAddr, Ipv4Addr, Ipv6Addr, Shutdown, SocketAddr};

library/std/src/sys/hermit/time.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#![allow(dead_code)]
22

33
use crate::cmp::Ordering;
4-
use crate::convert::TryInto;
54
use crate::sys::hermit::abi;
65
use crate::sys::hermit::abi::timespec;
76
use crate::sys::hermit::abi::{CLOCK_MONOTONIC, CLOCK_REALTIME, NSEC_PER_SEC};

library/std/src/sys/itron/spin.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use super::abi;
22
use crate::{
33
cell::UnsafeCell,
4-
convert::TryFrom,
54
mem::MaybeUninit,
65
sync::atomic::{AtomicBool, AtomicUsize, Ordering},
76
};

library/std/src/sys/itron/thread.rs

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use super::{
88
};
99
use crate::{
1010
cell::UnsafeCell,
11-
convert::TryFrom,
1211
ffi::CStr,
1312
hint, io,
1413
mem::ManuallyDrop,

0 commit comments

Comments
 (0)