Skip to content

Commit

Permalink
Auto merge of rust-lang#102627 - matthiaskrgr:rollup-2xtrqkw, r=matth…
Browse files Browse the repository at this point in the history
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#102439 (rustdoc: re-sugar more cross-crate trait bounds)
 - rust-lang#102569 (Improve `FromStr` example)
 - rust-lang#102597 (Avoid ICE in printing RPITIT type)
 - rust-lang#102607 (Improve documentation of `slice::{from_ptr_range, from_ptr_range_mut}`)
 - rust-lang#102613 (Fix ICE rust-lang#101739)
 - rust-lang#102615 (Cleanup some error code explanations)
 - rust-lang#102617 (`HirId` for `deferred_transmute_checks`)
 - rust-lang#102620 (Migrate `.stab` elements style to CSS variables)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
  • Loading branch information
bors committed Oct 3, 2022
2 parents 33d3519 + d329213 commit 0922559
Show file tree
Hide file tree
Showing 31 changed files with 325 additions and 86 deletions.
4 changes: 1 addition & 3 deletions compiler/rustc_error_codes/src/error_codes/E0045.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ Variadic parameters have been used on a non-C ABI function.
Erroneous code example:

```compile_fail,E0045
#![feature(unboxed_closures)]
extern "rust-call" {
extern "Rust" {
fn foo(x: u8, ...); // error!
}
```
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_error_codes/src/error_codes/E0092.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ functions are defined in `compiler/rustc_codegen_llvm/src/intrinsic.rs` and in
#![feature(intrinsics)]
extern "rust-intrinsic" {
fn atomic_fence(); // ok!
fn atomic_fence_seqcst(); // ok!
}
```
7 changes: 2 additions & 5 deletions compiler/rustc_error_codes/src/error_codes/E0161.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ A value was moved whose size was not known at compile time.
Erroneous code example:

```compile_fail,E0161
#![feature(box_syntax)]
trait Bar {
fn f(self);
}
Expand All @@ -13,7 +12,7 @@ impl Bar for i32 {
}
fn main() {
let b: Box<dyn Bar> = box (0 as i32);
let b: Box<dyn Bar> = Box::new(0i32);
b.f();
// error: cannot move a value of type dyn Bar: the size of dyn Bar cannot
// be statically determined
Expand All @@ -27,8 +26,6 @@ either `&x` or `&mut x`. Since a reference has a fixed size, this lets you move
it around as usual. Example:

```
#![feature(box_syntax)]
trait Bar {
fn f(&self);
}
Expand All @@ -38,7 +35,7 @@ impl Bar for i32 {
}
fn main() {
let b: Box<dyn Bar> = box (0 as i32);
let b: Box<dyn Bar> = Box::new(0i32);
b.f();
// ok!
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_error_codes/src/error_codes/E0579.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ Erroneous code example:
fn main() {
match 5u32 {
// This range is ok, albeit pointless.
1 .. 2 => {}
1..2 => {}
// This range is empty, and the compiler can tell.
5 .. 5 => {} // error!
5..5 => {} // error!
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_error_codes/src/error_codes/E0622.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Erroneous code example:
```compile_fail,E0622
#![feature(intrinsics)]
extern "rust-intrinsic" {
pub static breakpoint : fn(); // error: intrinsic must be a function
pub static breakpoint: fn(); // error: intrinsic must be a function
}
fn main() { unsafe { breakpoint(); } }
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_error_codes/src/error_codes/E0743.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ The C-variadic type `...` has been nested inside another type.
Erroneous code example:

```compile_fail,E0743
#![feature(c_variadic)]
fn foo2(x: u8, y: &...) {} // error!
```

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// been resolved or we errored. This is important as we can only check transmute
// on concrete types, but the output type may not be known yet (it would only
// be known if explicitly specified via turbofish).
self.deferred_transmute_checks.borrow_mut().push((from, to, expr.span));
self.deferred_transmute_checks.borrow_mut().push((from, to, expr.hir_id));
}
if !tcx.features().unsized_fn_params {
// We want to remove some Sized bounds from std functions,
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_hir_analysis/src/check/fn_ctxt/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
pub(in super::super) fn check_transmutes(&self) {
let mut deferred_transmute_checks = self.deferred_transmute_checks.borrow_mut();
debug!("FnCtxt::check_transmutes: {} deferred checks", deferred_transmute_checks.len());
for (from, to, span) in deferred_transmute_checks.drain(..) {
self.check_transmute(span, from, to);
for (from, to, hir_id) in deferred_transmute_checks.drain(..) {
self.check_transmute(from, to, hir_id);
}
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/check/inherited.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub struct Inherited<'a, 'tcx> {

pub(super) deferred_cast_checks: RefCell<Vec<super::cast::CastCheck<'tcx>>>,

pub(super) deferred_transmute_checks: RefCell<Vec<(Ty<'tcx>, Ty<'tcx>, Span)>>,
pub(super) deferred_transmute_checks: RefCell<Vec<(Ty<'tcx>, Ty<'tcx>, hir::HirId)>>,

pub(super) deferred_asm_checks: RefCell<Vec<(&'tcx hir::InlineAsm<'tcx>, hir::HirId)>>,

Expand Down
19 changes: 11 additions & 8 deletions compiler/rustc_hir_analysis/src/check/intrinsicck.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use hir::HirId;
use rustc_ast::InlineAsmTemplatePiece;
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::struct_span_err;
Expand All @@ -6,7 +7,7 @@ use rustc_index::vec::Idx;
use rustc_middle::ty::layout::{LayoutError, SizeSkeleton};
use rustc_middle::ty::{self, Article, FloatTy, IntTy, Ty, TyCtxt, TypeVisitable, UintTy};
use rustc_session::lint;
use rustc_span::{Span, Symbol, DUMMY_SP};
use rustc_span::{Symbol, DUMMY_SP};
use rustc_target::abi::{Pointer, VariantIdx};
use rustc_target::asm::{InlineAsmReg, InlineAsmRegClass, InlineAsmRegOrRegClass, InlineAsmType};

Expand Down Expand Up @@ -40,11 +41,13 @@ fn unpack_option_like<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Ty<'tcx> {
}

impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
pub fn check_transmute(&self, span: Span, from: Ty<'tcx>, to: Ty<'tcx>) {
pub fn check_transmute(&self, from: Ty<'tcx>, to: Ty<'tcx>, hir_id: HirId) {
let tcx = self.tcx;
let span = tcx.hir().span(hir_id);
let convert = |ty: Ty<'tcx>| {
let ty = self.resolve_vars_if_possible(ty);
let ty = self.tcx.normalize_erasing_regions(self.param_env, ty);
(SizeSkeleton::compute(ty, self.tcx, self.param_env), ty)
let ty = tcx.normalize_erasing_regions(self.param_env, ty);
(SizeSkeleton::compute(ty, tcx, self.param_env), ty)
};
let (sk_from, from) = convert(from);
let (sk_to, to) = convert(to);
Expand All @@ -57,9 +60,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {

// Special-case transmuting from `typeof(function)` and
// `Option<typeof(function)>` to present a clearer error.
let from = unpack_option_like(self.tcx, from);
if let (&ty::FnDef(..), SizeSkeleton::Known(size_to)) = (from.kind(), sk_to) && size_to == Pointer.size(&self.tcx) {
struct_span_err!(self.tcx.sess, span, E0591, "can't transmute zero-sized type")
let from = unpack_option_like(tcx, from);
if let (&ty::FnDef(..), SizeSkeleton::Known(size_to)) = (from.kind(), sk_to) && size_to == Pointer.size(&tcx) {
struct_span_err!(tcx.sess, span, E0591, "can't transmute zero-sized type")
.note(&format!("source type: {from}"))
.note(&format!("target type: {to}"))
.help("cast with `as` to a pointer instead")
Expand All @@ -83,7 +86,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
};

let mut err = struct_span_err!(
self.tcx.sess,
tcx.sess,
span,
E0512,
"cannot transmute between types of different sizes, \
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,7 @@ pub trait PrettyPrinter<'tcx>:
// unless we can find out what generator return type it comes from.
let term = if let Some(ty) = term.skip_binder().ty()
&& let ty::Projection(proj) = ty.kind()
&& let assoc = tcx.associated_item(proj.item_def_id)
&& let Some(assoc) = tcx.opt_associated_item(proj.item_def_id)
&& assoc.trait_container(tcx) == tcx.lang_items().gen_trait()
&& assoc.name == rustc_span::sym::Return
{
Expand Down
13 changes: 8 additions & 5 deletions compiler/rustc_trait_selection/src/traits/select/confirmation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ use rustc_hir::lang_items::LangItem;
use rustc_index::bit_set::GrowableBitSet;
use rustc_infer::infer::InferOk;
use rustc_infer::infer::LateBoundRegionConversionTime::HigherRankedType;
use rustc_middle::ty::{self, GenericParamDefKind, Ty, TyCtxt};
use rustc_middle::ty::{GenericArg, GenericArgKind, InternalSubsts, SubstsRef};
use rustc_middle::ty::{ToPolyTraitRef, ToPredicate};
use rustc_middle::ty::{
self, GenericArg, GenericArgKind, GenericParamDefKind, InternalSubsts, SubstsRef,
ToPolyTraitRef, ToPredicate, Ty, TyCtxt,
};
use rustc_span::def_id::DefId;

use crate::traits::project::{normalize_with_depth, normalize_with_depth_to};
Expand Down Expand Up @@ -289,8 +290,10 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {

let scope = type_at(2).skip_binder();

let assume =
rustc_transmute::Assume::from_const(self.infcx.tcx, obligation.param_env, const_at(3));
let Some(assume) =
rustc_transmute::Assume::from_const(self.infcx.tcx, obligation.param_env, const_at(3)) else {
return Err(Unimplemented);
};

let cause = obligation.cause.clone();

Expand Down
15 changes: 10 additions & 5 deletions compiler/rustc_transmute/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,23 @@ mod rustc {
tcx: TyCtxt<'tcx>,
param_env: ParamEnv<'tcx>,
c: Const<'tcx>,
) -> Self {
) -> Option<Self> {
use rustc_middle::ty::ScalarInt;
use rustc_middle::ty::TypeVisitable;
use rustc_span::symbol::sym;

let c = c.eval(tcx, param_env);

if let Some(err) = c.error_reported() {
return Self { alignment: true, lifetimes: true, safety: true, validity: true };
return Some(Self {
alignment: true,
lifetimes: true,
safety: true,
validity: true,
});
}

let adt_def = c.ty().ty_adt_def().expect("The given `Const` must be an ADT.");
let adt_def = c.ty().ty_adt_def()?;

assert_eq!(
tcx.require_lang_item(LangItem::TransmuteOpts, None),
Expand All @@ -148,12 +153,12 @@ mod rustc {
fields[field_idx].unwrap_leaf() == ScalarInt::TRUE
};

Self {
Some(Self {
alignment: get_field(sym::alignment),
lifetimes: get_field(sym::lifetimes),
safety: get_field(sym::safety),
validity: get_field(sym::validity),
}
})
}
}
}
Expand Down
24 changes: 23 additions & 1 deletion library/core/src/slice/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ pub const fn from_mut<T>(s: &mut T) -> &mut [T] {
///
/// Note that a range created from [`slice::as_ptr_range`] fulfills these requirements.
///
/// # Panics
///
/// This function panics if `T` is a Zero-Sized Type (“ZST”).
///
/// # Caveat
///
/// The lifetime for the returned slice is inferred from its usage. To
Expand Down Expand Up @@ -219,9 +223,15 @@ pub const unsafe fn from_ptr_range<'a, T>(range: Range<*const T>) -> &'a [T] {
unsafe { from_raw_parts(range.start, range.end.sub_ptr(range.start)) }
}

/// Performs the same functionality as [`from_ptr_range`], except that a
/// Forms a mutable slice from a pointer range.
///
/// This is the same functionality as [`from_ptr_range`], except that a
/// mutable slice is returned.
///
/// This function is useful for interacting with foreign interfaces which
/// use two pointers to refer to a range of elements in memory, as is
/// common in C++.
///
/// # Safety
///
/// Behavior is undefined if any of the following conditions are violated:
Expand All @@ -247,6 +257,18 @@ pub const unsafe fn from_ptr_range<'a, T>(range: Range<*const T>) -> &'a [T] {
///
/// Note that a range created from [`slice::as_mut_ptr_range`] fulfills these requirements.
///
/// # Panics
///
/// This function panics if `T` is a Zero-Sized Type (“ZST”).
///
/// # Caveat
///
/// The lifetime for the returned slice is inferred from its usage. To
/// prevent accidental misuse, it's suggested to tie the lifetime to whichever
/// source lifetime is safe in the context, such as by providing a helper
/// function taking the lifetime of a host value for the slice, or by explicit
/// annotation.
///
/// # Examples
///
/// ```
Expand Down
14 changes: 9 additions & 5 deletions library/core/src/str/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,26 +507,28 @@ unsafe impl const SliceIndex<str> for ops::RangeToInclusive<usize> {
///
/// ```
/// use std::str::FromStr;
/// use std::num::ParseIntError;
///
/// #[derive(Debug, PartialEq)]
/// struct Point {
/// x: i32,
/// y: i32
/// }
///
/// #[derive(Debug, PartialEq, Eq)]
/// struct ParsePointError;
///
/// impl FromStr for Point {
/// type Err = ParseIntError;
/// type Err = ParsePointError;
///
/// fn from_str(s: &str) -> Result<Self, Self::Err> {
/// let (x, y) = s
/// .strip_prefix('(')
/// .and_then(|s| s.strip_suffix(')'))
/// .and_then(|s| s.split_once(','))
/// .unwrap();
/// .ok_or(ParsePointError)?;
///
/// let x_fromstr = x.parse::<i32>()?;
/// let y_fromstr = y.parse::<i32>()?;
/// let x_fromstr = x.parse::<i32>().map_err(|_| ParsePointError)?;
/// let y_fromstr = y.parse::<i32>().map_err(|_| ParsePointError)?;
///
/// Ok(Point { x: x_fromstr, y: y_fromstr })
/// }
Expand All @@ -538,6 +540,8 @@ unsafe impl const SliceIndex<str> for ops::RangeToInclusive<usize> {
/// // Implicit calls, through parse
/// assert_eq!("(1,2)".parse(), expected);
/// assert_eq!("(1,2)".parse::<Point>(), expected);
/// // Invalid input string
/// assert!(Point::from_str("(1 2)").is_err());
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub trait FromStr: Sized {
Expand Down
9 changes: 9 additions & 0 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,15 @@ pub(crate) fn clean_middle_assoc_item<'tcx>(
}

if let ty::TraitContainer = assoc_item.container {
// FIXME(fmease): `tcx.explicit_item_bounds` does not contain the bounds of GATs,
// e.g. the bounds `Copy`, `Display` & (implicitly) `Sized` in
// `type Assoc<T: Copy> where T: Display`. This also means that we
// later incorrectly render `where T: ?Sized`.
//
// The result of `tcx.explicit_predicates_of` *does* contain them but
// it does not contain the other bounds / predicates we need.
// Either merge those two interned lists somehow or refactor
// `clean_ty_generics` to call `explicit_item_bounds` by itself.
let bounds = tcx.explicit_item_bounds(assoc_item.def_id);
let predicates = ty::GenericPredicates { parent: None, predicates: bounds };
let mut generics =
Expand Down
Loading

0 comments on commit 0922559

Please sign in to comment.