Skip to content

Commit

Permalink
Auto merge of rust-lang#126297 - jhpratt:rollup-ntic8q6, r=jhpratt
Browse files Browse the repository at this point in the history
Rollup of 6 pull requests

Successful merges:

 - rust-lang#123374 (DOC: Add FFI example for slice::from_raw_parts())
 - rust-lang#126127 (Spell out other trait diagnostic)
 - rust-lang#126228 (Provide correct parent for nested anon const)
 - rust-lang#126249 (Simplify `[T; N]::try_map` signature)
 - rust-lang#126256 (Add {{target}} substitution to compiletest)
 - rust-lang#126263 (Make issue-122805.rs big endian compatible)

r? `@ghost`
`@rustbot` modify labels: rollup
  • Loading branch information
bors committed Jun 12, 2024
2 parents 9a7bf4a + 8732f19 commit 6a0e442
Show file tree
Hide file tree
Showing 48 changed files with 450 additions and 362 deletions.
26 changes: 21 additions & 5 deletions compiler/rustc_hir_analysis/src/collect/generics_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use rustc_session::lint;
use rustc_span::symbol::{kw, Symbol};
use rustc_span::Span;

#[instrument(level = "debug", skip(tcx))]
pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
use rustc_hir::*;

Expand Down Expand Up @@ -66,7 +67,22 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
// FIXME(#43408) always enable this once `lazy_normalization` is
// stable enough and does not need a feature gate anymore.
Node::AnonConst(_) => {
let parent_def_id = tcx.hir().get_parent_item(hir_id);
let parent_did = tcx.parent(def_id.to_def_id());

// We don't do this unconditionally because the `DefId` parent of an anon const
// might be an implicitly created closure during `async fn` desugaring. This would
// have the wrong generics.
//
// i.e. `async fn foo<'a>() { let a = [(); { 1 + 2 }]; bar().await() }`
// would implicitly have a closure in its body that would be the parent of
// the `{ 1 + 2 }` anon const. This closure's generics is simply a witness
// instead of `['a]`.
let parent_did = if let DefKind::AnonConst = tcx.def_kind(parent_did) {
parent_did
} else {
tcx.hir().get_parent_item(hir_id).to_def_id()
};
debug!(?parent_did);

let mut in_param_ty = false;
for (_parent, node) in tcx.hir().parent_iter(hir_id) {
Expand Down Expand Up @@ -121,7 +137,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
//
// This has some implications for how we get the predicates available to the anon const
// see `explicit_predicates_of` for more information on this
let generics = tcx.generics_of(parent_def_id.to_def_id());
let generics = tcx.generics_of(parent_did);
let param_def_idx = generics.param_def_id_to_index[&param_id.to_def_id()];
// In the above example this would be .params[..N#0]
let own_params = generics.params_to(param_def_idx as usize, tcx).to_owned();
Expand All @@ -147,7 +163,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
//
// Note that we do not supply the parent generics when using
// `min_const_generics`.
Some(parent_def_id.to_def_id())
Some(parent_did)
}
} else {
let parent_node = tcx.parent_hir_node(hir_id);
Expand All @@ -159,7 +175,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
Node::Expr(Expr { kind: ExprKind::Repeat(_, constant), .. })
if constant.hir_id() == hir_id =>
{
Some(parent_def_id.to_def_id())
Some(parent_did)
}
// Exclude `GlobalAsm` here which cannot have generics.
Node::Expr(&Expr { kind: ExprKind::InlineAsm(asm), .. })
Expand All @@ -171,7 +187,7 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
_ => false,
}) =>
{
Some(parent_def_id.to_def_id())
Some(parent_did)
}
_ => None,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2064,7 +2064,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
if all_traits_equal {
format!("\n {}", c.self_ty())
} else {
format!("\n {c}")
format!("\n `{}` implements `{}`", c.self_ty(), c.print_only_trait_path())
}
})
.collect();
Expand Down
6 changes: 2 additions & 4 deletions library/core/src/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,11 +533,9 @@ impl<T, const N: usize> [T; N] {
/// assert_eq!(c, Some(a));
/// ```
#[unstable(feature = "array_try_map", issue = "79711")]
pub fn try_map<F, R>(self, f: F) -> ChangeOutputType<R, [R::Output; N]>
pub fn try_map<R>(self, f: impl FnMut(T) -> R) -> ChangeOutputType<R, [R::Output; N]>
where
F: FnMut(T) -> R,
R: Try,
R::Residual: Residual<[R::Output; N]>,
R: Try<Residual: Residual<[R::Output; N]>>,
{
drain_array_with(self, |iter| try_from_trusted_iterator(iter.map(f)))
}
Expand Down
4 changes: 3 additions & 1 deletion library/core/src/ops/try_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,9 @@ pub trait Residual<O> {
}

#[unstable(feature = "pub_crate_should_not_need_unstable_attr", issue = "none")]
pub(crate) type ChangeOutputType<T, V> = <<T as Try>::Residual as Residual<V>>::TryType;
#[allow(type_alias_bounds)]
pub(crate) type ChangeOutputType<T: Try<Residual: Residual<V>>, V> =
<T::Residual as Residual<V>>::TryType;

/// An adapter for implementing non-try methods via the `Try` implementation.
///
Expand Down
33 changes: 33 additions & 0 deletions library/core/src/slice/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,39 @@ use crate::ub_checks;
/// }
/// ```
///
/// ### FFI: Handling null pointers
///
/// In languages such as C++, pointers to empty collections are not guaranteed to be non-null.
/// When accepting such pointers, they have to be checked for null-ness to avoid undefined
/// behavior.
///
/// ```
/// use std::slice;
///
/// /// Sum the elements of an FFI slice.
/// ///
/// /// # Safety
/// ///
/// /// If ptr is not NULL, it must be correctly aligned and
/// /// point to `len` initialized items of type `f32`.
/// unsafe extern "C" fn sum_slice(ptr: *const f32, len: usize) -> f32 {
/// let data = if ptr.is_null() {
/// // `len` is assumed to be 0.
/// &[]
/// } else {
/// // SAFETY: see function docstring.
/// unsafe { slice::from_raw_parts(ptr, len) }
/// };
/// data.into_iter().sum()
/// }
///
/// // This could be the result of C++'s std::vector::data():
/// let ptr = std::ptr::null();
/// // And this could be std::vector::size():
/// let len = 0;
/// assert_eq!(unsafe { sum_slice(ptr, len) }, 0.0);
/// ```
///
/// [valid]: ptr#safety
/// [`NonNull::dangling()`]: ptr::NonNull::dangling
#[inline]
Expand Down
5 changes: 5 additions & 0 deletions src/tools/compiletest/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1281,6 +1281,7 @@ fn expand_variables(mut value: String, config: &Config) -> String {
const BUILD_BASE: &str = "{{build-base}}";
const SYSROOT_BASE: &str = "{{sysroot-base}}";
const TARGET_LINKER: &str = "{{target-linker}}";
const TARGET: &str = "{{target}}";

if value.contains(CWD) {
let cwd = env::current_dir().unwrap();
Expand All @@ -1303,6 +1304,10 @@ fn expand_variables(mut value: String, config: &Config) -> String {
value = value.replace(TARGET_LINKER, config.target_linker.as_deref().unwrap_or(""));
}

if value.contains(TARGET) {
value = value.replace(TARGET, &config.target);
}

value
}

Expand Down
6 changes: 3 additions & 3 deletions src/tools/tidy/src/target_specific_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ pub fn check(path: &Path, bad: &mut bool) {
} else if directive.starts_with(COMPILE_FLAGS_HEADER) {
let compile_flags = &directive[COMPILE_FLAGS_HEADER.len()..];
if let Some((_, v)) = compile_flags.split_once("--target") {
if let Some((arch, _)) =
v.trim_start_matches(|c| c == ' ' || c == '=').split_once("-")
{
let v = v.trim_start_matches(|c| c == ' ' || c == '=');
let v = if v == "{{target}}" { Some((v, v)) } else { v.split_once("-") };
if let Some((arch, _)) = v {
let info = header_map.entry(revision).or_insert(RevisionInfo::default());
info.target_arch.replace(arch);
} else {
Expand Down
21 changes: 12 additions & 9 deletions tests/codegen/issues/issue-122805.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,20 @@
// OPT3WINX64-NEXT: store <8 x i16>
// CHECK-NEXT: ret void
#[no_mangle]
#[cfg(target_endian = "little")]
pub fn convert(value: [u16; 8]) -> [u8; 16] {
#[cfg(target_endian = "little")]
let bswap = u16::to_be;
#[cfg(target_endian = "big")]
let bswap = u16::to_le;
let addr16 = [
value[0].to_be(),
value[1].to_be(),
value[2].to_be(),
value[3].to_be(),
value[4].to_be(),
value[5].to_be(),
value[6].to_be(),
value[7].to_be(),
bswap(value[0]),
bswap(value[1]),
bswap(value[2]),
bswap(value[3]),
bswap(value[4]),
bswap(value[5]),
bswap(value[6]),
bswap(value[7]),
];
unsafe { core::mem::transmute::<_, [u8; 16]>(addr16) }
}
8 changes: 4 additions & 4 deletions tests/ui/binop/binary-op-suggest-deref.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,10 @@ LL | let _ = FOO & (*"Sized".to_string().into_boxed_str());
|
= help: the trait `BitAnd<str>` is not implemented for `i32`
= help: the following other types implement trait `BitAnd<Rhs>`:
<&'a i32 as BitAnd<i32>>
<&i32 as BitAnd<&i32>>
<i32 as BitAnd<&i32>>
<i32 as BitAnd>
`&'a i32` implements `BitAnd<i32>`
`&i32` implements `BitAnd<&i32>`
`i32` implements `BitAnd<&i32>`
`i32` implements `BitAnd`

error[E0277]: the size for values of type `str` cannot be known at compilation time
--> $DIR/binary-op-suggest-deref.rs:78:17
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/binop/binop-mul-i32-f32.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ LL | x * y
|
= help: the trait `Mul<f32>` is not implemented for `i32`
= help: the following other types implement trait `Mul<Rhs>`:
<&'a i32 as Mul<i32>>
<&i32 as Mul<&i32>>
<i32 as Mul<&i32>>
<i32 as Mul>
`&'a i32` implements `Mul<i32>`
`&i32` implements `Mul<&i32>`
`i32` implements `Mul<&i32>`
`i32` implements `Mul`

error: aborting due to 1 previous error

Expand Down
48 changes: 24 additions & 24 deletions tests/ui/binop/shift-various-bad-types.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ LL | 22 >> p.char;
|
= help: the trait `Shr<char>` is not implemented for `{integer}`
= help: the following other types implement trait `Shr<Rhs>`:
<&'a i128 as Shr<i128>>
<&'a i128 as Shr<i16>>
<&'a i128 as Shr<i32>>
<&'a i128 as Shr<i64>>
<&'a i128 as Shr<i8>>
<&'a i128 as Shr<isize>>
<&'a i128 as Shr<u128>>
<&'a i128 as Shr<u16>>
`&'a i128` implements `Shr<i128>`
`&'a i128` implements `Shr<i16>`
`&'a i128` implements `Shr<i32>`
`&'a i128` implements `Shr<i64>`
`&'a i128` implements `Shr<i8>`
`&'a i128` implements `Shr<isize>`
`&'a i128` implements `Shr<u128>`
`&'a i128` implements `Shr<u16>`
and 568 others

error[E0277]: no implementation for `{integer} >> &str`
Expand All @@ -24,14 +24,14 @@ LL | 22 >> p.str;
|
= help: the trait `Shr<&str>` is not implemented for `{integer}`
= help: the following other types implement trait `Shr<Rhs>`:
<&'a i128 as Shr<i128>>
<&'a i128 as Shr<i16>>
<&'a i128 as Shr<i32>>
<&'a i128 as Shr<i64>>
<&'a i128 as Shr<i8>>
<&'a i128 as Shr<isize>>
<&'a i128 as Shr<u128>>
<&'a i128 as Shr<u16>>
`&'a i128` implements `Shr<i128>`
`&'a i128` implements `Shr<i16>`
`&'a i128` implements `Shr<i32>`
`&'a i128` implements `Shr<i64>`
`&'a i128` implements `Shr<i8>`
`&'a i128` implements `Shr<isize>`
`&'a i128` implements `Shr<u128>`
`&'a i128` implements `Shr<u16>`
and 568 others

error[E0277]: no implementation for `{integer} >> &Panolpy`
Expand All @@ -42,14 +42,14 @@ LL | 22 >> p;
|
= help: the trait `Shr<&Panolpy>` is not implemented for `{integer}`
= help: the following other types implement trait `Shr<Rhs>`:
<&'a i128 as Shr<i128>>
<&'a i128 as Shr<i16>>
<&'a i128 as Shr<i32>>
<&'a i128 as Shr<i64>>
<&'a i128 as Shr<i8>>
<&'a i128 as Shr<isize>>
<&'a i128 as Shr<u128>>
<&'a i128 as Shr<u16>>
`&'a i128` implements `Shr<i128>`
`&'a i128` implements `Shr<i16>`
`&'a i128` implements `Shr<i32>`
`&'a i128` implements `Shr<i64>`
`&'a i128` implements `Shr<i8>`
`&'a i128` implements `Shr<isize>`
`&'a i128` implements `Shr<u128>`
`&'a i128` implements `Shr<u16>`
and 568 others

error[E0308]: mismatched types
Expand Down
16 changes: 8 additions & 8 deletions tests/ui/const-generics/exhaustive-value.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ LL | <() as Foo<N>>::test()
| ^^ the trait `Foo<N>` is not implemented for `()`
|
= help: the following other types implement trait `Foo<N>`:
<() as Foo<0>>
<() as Foo<100>>
<() as Foo<101>>
<() as Foo<102>>
<() as Foo<103>>
<() as Foo<104>>
<() as Foo<105>>
<() as Foo<106>>
`()` implements `Foo<0>`
`()` implements `Foo<100>`
`()` implements `Foo<101>`
`()` implements `Foo<102>`
`()` implements `Foo<103>`
`()` implements `Foo<104>`
`()` implements `Foo<105>`
`()` implements `Foo<106>`
and 248 others

error: aborting due to 1 previous error
Expand Down
20 changes: 20 additions & 0 deletions tests/ui/const-generics/repeat_expr_hack_gives_right_generics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Given an anon const `a`: `{ N }` and some anon const `b` which references the
// first anon const: `{ [1; a] }`. `b` should not have any generics as it is not
// a simple `N` argument nor is it a repeat expr count.
//
// On the other hand `b` *is* a repeat expr count and so it should inherit its
// parents generics as part of the `const_evaluatable_unchecked` fcw (#76200).
//
// In this specific case however `b`'s parent should be `a` and so it should wind
// up not having any generics after all. If `a` were to inherit its generics from
// the enclosing item then the reference to `a` from `b` would contain generic
// parameters not usable by `b` which would cause us to ICE.

fn bar<const N: usize>() {}

fn foo<const N: usize>() {
bar::<{ [1; N] }>();
//~^ ERROR: generic parameters may not be used in const operations
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error: generic parameters may not be used in const operations
--> $DIR/repeat_expr_hack_gives_right_generics.rs:16:17
|
LL | bar::<{ [1; N] }>();
| ^ cannot perform const operation using `N`
|
= help: const parameters may only be used as standalone arguments, i.e. `N`
= help: add `#![feature(generic_const_exprs)]` to allow generic const expressions

error: aborting due to 1 previous error

8 changes: 4 additions & 4 deletions tests/ui/consts/const-eval/const-eval-overflow-3b.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ LL | = [0; (i8::MAX + 1u8) as usize];
|
= help: the trait `Add<u8>` is not implemented for `i8`
= help: the following other types implement trait `Add<Rhs>`:
<&'a i8 as Add<i8>>
<&i8 as Add<&i8>>
<i8 as Add<&i8>>
<i8 as Add>
`&'a i8` implements `Add<i8>`
`&i8` implements `Add<&i8>`
`i8` implements `Add<&i8>`
`i8` implements `Add`

error: aborting due to 2 previous errors

Expand Down
8 changes: 4 additions & 4 deletions tests/ui/consts/const-eval/const-eval-overflow-4b.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ LL | : [u32; (i8::MAX as i8 + 1u8) as usize]
|
= help: the trait `Add<u8>` is not implemented for `i8`
= help: the following other types implement trait `Add<Rhs>`:
<&'a i8 as Add<i8>>
<&i8 as Add<&i8>>
<i8 as Add<&i8>>
<i8 as Add>
`&'a i8` implements `Add<i8>`
`&i8` implements `Add<&i8>`
`i8` implements `Add<&i8>`
`i8` implements `Add`

error[E0604]: only `u8` can be cast as `char`, not `i8`
--> $DIR/const-eval-overflow-4b.rs:22:13
Expand Down
Loading

0 comments on commit 6a0e442

Please sign in to comment.