From d3e5177f816d723cbce31ae7a7779feca8a69724 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Kr=C3=BCger?= Date: Tue, 3 Mar 2020 01:19:00 +0100 Subject: [PATCH 1/6] Use .next() instead of .nth(0) on iterators. --- src/librustc/mir/mod.rs | 2 +- src/librustc/ty/util.rs | 2 +- src/librustc_mir/transform/simplify.rs | 2 +- src/librustc_mir/util/elaborate_drops.rs | 2 +- src/librustc_mir_build/hair/pattern/_match.rs | 2 +- src/librustc_parse/parser/expr.rs | 2 +- src/librustc_parse/parser/pat.rs | 2 +- src/librustc_parse/parser/ty.rs | 2 +- src/librustc_span/source_map.rs | 6 +++--- src/librustc_typeck/check/closure.rs | 2 +- src/librustc_typeck/check/mod.rs | 2 +- src/tools/unicode-table-generator/src/main.rs | 2 +- 12 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/librustc/mir/mod.rs b/src/librustc/mir/mod.rs index 668240ab42b4c..bb2d57c856d4b 100644 --- a/src/librustc/mir/mod.rs +++ b/src/librustc/mir/mod.rs @@ -1446,7 +1446,7 @@ impl<'tcx> Debug for TerminatorKind<'tcx> { match successor_count { 0 => Ok(()), - 1 => write!(fmt, " -> {:?}", self.successors().nth(0).unwrap()), + 1 => write!(fmt, " -> {:?}", self.successors().next().unwrap()), _ => { write!(fmt, " -> [")?; diff --git a/src/librustc/ty/util.rs b/src/librustc/ty/util.rs index 7b1f821877baa..87dcc617fa289 100644 --- a/src/librustc/ty/util.rs +++ b/src/librustc/ty/util.rs @@ -357,7 +357,7 @@ impl<'tcx> TyCtxt<'tcx> { let mut dtor_did = None; let ty = self.type_of(adt_did); self.for_each_relevant_impl(drop_trait, ty, |impl_did| { - if let Some(item) = self.associated_items(impl_did).in_definition_order().nth(0) { + if let Some(item) = self.associated_items(impl_did).in_definition_order().next() { if validate(self, impl_did).is_ok() { dtor_did = Some(item.def_id); } diff --git a/src/librustc_mir/transform/simplify.rs b/src/librustc_mir/transform/simplify.rs index ddf8d73e5481f..597d3f0237a71 100644 --- a/src/librustc_mir/transform/simplify.rs +++ b/src/librustc_mir/transform/simplify.rs @@ -230,7 +230,7 @@ impl<'a, 'tcx> CfgSimplifier<'a, 'tcx> { }; let first_succ = { - if let Some(&first_succ) = terminator.successors().nth(0) { + if let Some(&first_succ) = terminator.successors().next() { if terminator.successors().all(|s| *s == first_succ) { let count = terminator.successors().count(); self.pred_count[first_succ] -= (count - 1) as u32; diff --git a/src/librustc_mir/util/elaborate_drops.rs b/src/librustc_mir/util/elaborate_drops.rs index f7239ae55faa2..a928ab6a00979 100644 --- a/src/librustc_mir/util/elaborate_drops.rs +++ b/src/librustc_mir/util/elaborate_drops.rs @@ -549,7 +549,7 @@ where debug!("destructor_call_block({:?}, {:?})", self, succ); let tcx = self.tcx(); let drop_trait = tcx.lang_items().drop_trait().unwrap(); - let drop_fn = tcx.associated_items(drop_trait).in_definition_order().nth(0).unwrap(); + let drop_fn = tcx.associated_items(drop_trait).in_definition_order().next().unwrap(); let ty = self.place_ty(self.place); let substs = tcx.mk_substs_trait(ty, &[]); diff --git a/src/librustc_mir_build/hair/pattern/_match.rs b/src/librustc_mir_build/hair/pattern/_match.rs index 90e4f53647846..64960a0da9cde 100644 --- a/src/librustc_mir_build/hair/pattern/_match.rs +++ b/src/librustc_mir_build/hair/pattern/_match.rs @@ -1000,7 +1000,7 @@ impl<'tcx> Constructor<'tcx> { PatKind::Leaf { subpatterns } } } - ty::Ref(..) => PatKind::Deref { subpattern: subpatterns.nth(0).unwrap() }, + ty::Ref(..) => PatKind::Deref { subpattern: subpatterns.next().unwrap() }, ty::Slice(_) | ty::Array(..) => bug!("bad slice pattern {:?} {:?}", self, ty), _ => PatKind::Wild, }, diff --git a/src/librustc_parse/parser/expr.rs b/src/librustc_parse/parser/expr.rs index 18ddd23588e48..14366eb4e8748 100644 --- a/src/librustc_parse/parser/expr.rs +++ b/src/librustc_parse/parser/expr.rs @@ -955,7 +955,7 @@ impl<'a> Parser<'a> { }; let kind = if es.len() == 1 && !trailing_comma { // `(e)` is parenthesized `e`. - ExprKind::Paren(es.into_iter().nth(0).unwrap()) + ExprKind::Paren(es.into_iter().next().unwrap()) } else { // `(e,)` is a tuple with only one field, `e`. ExprKind::Tup(es) diff --git a/src/librustc_parse/parser/pat.rs b/src/librustc_parse/parser/pat.rs index 45d1aacdd3cc6..4c041fd669d67 100644 --- a/src/librustc_parse/parser/pat.rs +++ b/src/librustc_parse/parser/pat.rs @@ -479,7 +479,7 @@ impl<'a> Parser<'a> { // Here, `(pat,)` is a tuple pattern. // For backward compatibility, `(..)` is a tuple pattern as well. Ok(if fields.len() == 1 && !(trailing_comma || fields[0].is_rest()) { - PatKind::Paren(fields.into_iter().nth(0).unwrap()) + PatKind::Paren(fields.into_iter().next().unwrap()) } else { PatKind::Tuple(fields) }) diff --git a/src/librustc_parse/parser/ty.rs b/src/librustc_parse/parser/ty.rs index 3d2b0c014ac49..c4469331b6669 100644 --- a/src/librustc_parse/parser/ty.rs +++ b/src/librustc_parse/parser/ty.rs @@ -198,7 +198,7 @@ impl<'a> Parser<'a> { })?; if ts.len() == 1 && !trailing { - let ty = ts.into_iter().nth(0).unwrap().into_inner(); + let ty = ts.into_iter().next().unwrap().into_inner(); let maybe_bounds = allow_plus == AllowPlus::Yes && self.token.is_like_plus(); match ty.kind { // `(TY_BOUND_NOPAREN) + BOUND + ...`. diff --git a/src/librustc_span/source_map.rs b/src/librustc_span/source_map.rs index 39601ad76222c..798600fc68275 100644 --- a/src/librustc_span/source_map.rs +++ b/src/librustc_span/source_map.rs @@ -620,7 +620,7 @@ impl SourceMap { /// if no character could be found or if an error occurred while retrieving the code snippet. pub fn span_extend_to_prev_char(&self, sp: Span, c: char) -> Span { if let Ok(prev_source) = self.span_to_prev_source(sp) { - let prev_source = prev_source.rsplit(c).nth(0).unwrap_or("").trim_start(); + let prev_source = prev_source.rsplit(c).next().unwrap_or("").trim_start(); if !prev_source.is_empty() && !prev_source.contains('\n') { return sp.with_lo(BytePos(sp.lo().0 - prev_source.len() as u32)); } @@ -640,7 +640,7 @@ impl SourceMap { for ws in &[" ", "\t", "\n"] { let pat = pat.to_owned() + ws; if let Ok(prev_source) = self.span_to_prev_source(sp) { - let prev_source = prev_source.rsplit(&pat).nth(0).unwrap_or("").trim_start(); + let prev_source = prev_source.rsplit(&pat).next().unwrap_or("").trim_start(); if !prev_source.is_empty() && (!prev_source.contains('\n') || accept_newlines) { return sp.with_lo(BytePos(sp.lo().0 - prev_source.len() as u32)); } @@ -655,7 +655,7 @@ impl SourceMap { pub fn span_until_char(&self, sp: Span, c: char) -> Span { match self.span_to_snippet(sp) { Ok(snippet) => { - let snippet = snippet.split(c).nth(0).unwrap_or("").trim_end(); + let snippet = snippet.split(c).next().unwrap_or("").trim_end(); if !snippet.is_empty() && !snippet.contains('\n') { sp.with_hi(BytePos(sp.lo().0 + snippet.len() as u32)) } else { diff --git a/src/librustc_typeck/check/closure.rs b/src/librustc_typeck/check/closure.rs index 816de5dadbc15..8689db1b1eb56 100644 --- a/src/librustc_typeck/check/closure.rs +++ b/src/librustc_typeck/check/closure.rs @@ -675,7 +675,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // The `Future` trait has only one associted item, `Output`, // so check that this is what we see. let output_assoc_item = - self.tcx.associated_items(future_trait).in_definition_order().nth(0).unwrap().def_id; + self.tcx.associated_items(future_trait).in_definition_order().next().unwrap().def_id; if output_assoc_item != predicate.projection_ty.item_def_id { span_bug!( cause_span, diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index b7353c6af267a..f00476fb991f2 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -5244,7 +5244,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { .tcx .associated_items(future_trait) .in_definition_order() - .nth(0) + .next() .unwrap() .def_id; let predicate = diff --git a/src/tools/unicode-table-generator/src/main.rs b/src/tools/unicode-table-generator/src/main.rs index be8508e3973a2..839d914baa954 100644 --- a/src/tools/unicode-table-generator/src/main.rs +++ b/src/tools/unicode-table-generator/src/main.rs @@ -147,7 +147,7 @@ fn main() { eprintln!("Must provide path to write unicode tables to"); eprintln!( "e.g. {} src/libcore/unicode/unicode_data.rs", - std::env::args().nth(0).unwrap_or_default() + std::env::args().next().unwrap_or_default() ); std::process::exit(1); }); From c745b4a1841bc5bea8dd50cad85921b36ca47242 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 4 Mar 2020 13:01:19 +0100 Subject: [PATCH 2/6] Add explanation for E0380 --- src/librustc_error_codes/error_codes/E0380.md | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/librustc_error_codes/error_codes/E0380.md b/src/librustc_error_codes/error_codes/E0380.md index fe5de56933963..638f0c8ecc65f 100644 --- a/src/librustc_error_codes/error_codes/E0380.md +++ b/src/librustc_error_codes/error_codes/E0380.md @@ -1,4 +1,14 @@ -Auto traits cannot have methods or associated items. -For more information see the [opt-in builtin traits RFC][RFC 19]. +An auto trait was declared with a method or an associated item. + +Erroneous code example: + +```compile_fail,E0380 +unsafe auto trait Trait { + type Output; // error! +} +``` + +Auto traits cannot have methods or associated items. For more information see +the [opt-in builtin traits RFC][RFC 19]. [RFC 19]: https://github.com/rust-lang/rfcs/blob/master/text/0019-opt-in-builtin-traits.md From 6db7e34ab526abdadbff4e85701f55ce707687b8 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 4 Mar 2020 13:12:04 +0100 Subject: [PATCH 3/6] use integer assoc consts instead of methods --- src/librustc/mir/interpret/allocation.rs | 14 +++++++------- src/librustc/mir/interpret/pointer.rs | 4 ++-- src/librustc_mir/interpret/intrinsics.rs | 6 +++--- src/librustc_mir/interpret/validity.rs | 2 +- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/librustc/mir/interpret/allocation.rs b/src/librustc/mir/interpret/allocation.rs index d3efe62e8c153..c8d35db0adeb2 100644 --- a/src/librustc/mir/interpret/allocation.rs +++ b/src/librustc/mir/interpret/allocation.rs @@ -818,9 +818,9 @@ impl UndefMask { // First set all bits except the first `bita`, // then unset the last `64 - bitb` bits. let range = if bitb == 0 { - u64::max_value() << bita + u64::MAX << bita } else { - (u64::max_value() << bita) & (u64::max_value() >> (64 - bitb)) + (u64::MAX << bita) & (u64::MAX >> (64 - bitb)) }; if new_state { self.blocks[blocka] |= range; @@ -832,21 +832,21 @@ impl UndefMask { // across block boundaries if new_state { // Set `bita..64` to `1`. - self.blocks[blocka] |= u64::max_value() << bita; + self.blocks[blocka] |= u64::MAX << bita; // Set `0..bitb` to `1`. if bitb != 0 { - self.blocks[blockb] |= u64::max_value() >> (64 - bitb); + self.blocks[blockb] |= u64::MAX >> (64 - bitb); } // Fill in all the other blocks (much faster than one bit at a time). for block in (blocka + 1)..blockb { - self.blocks[block] = u64::max_value(); + self.blocks[block] = u64::MAX; } } else { // Set `bita..64` to `0`. - self.blocks[blocka] &= !(u64::max_value() << bita); + self.blocks[blocka] &= !(u64::MAX << bita); // Set `0..bitb` to `0`. if bitb != 0 { - self.blocks[blockb] &= !(u64::max_value() >> (64 - bitb)); + self.blocks[blockb] &= !(u64::MAX >> (64 - bitb)); } // Fill in all the other blocks (much faster than one bit at a time). for block in (blocka + 1)..blockb { diff --git a/src/librustc/mir/interpret/pointer.rs b/src/librustc/mir/interpret/pointer.rs index a4974fb541b6b..cc3c50b7899f3 100644 --- a/src/librustc/mir/interpret/pointer.rs +++ b/src/librustc/mir/interpret/pointer.rs @@ -78,9 +78,9 @@ pub trait PointerArithmetic: layout::HasDataLayout { fn overflowing_signed_offset(&self, val: u64, i: i128) -> (u64, bool) { // FIXME: is it possible to over/underflow here? if i < 0 { - // Trickery to ensure that `i64::min_value()` works fine: compute `n = -i`. + // Trickery to ensure that `i64::MIN` works fine: compute `n = -i`. // This formula only works for true negative values; it overflows for zero! - let n = u64::max_value() - (i as u64) + 1; + let n = u64::MAX - (i as u64) + 1; let res = val.overflowing_sub(n); self.truncate_to_ptr(res) } else { diff --git a/src/librustc_mir/interpret/intrinsics.rs b/src/librustc_mir/interpret/intrinsics.rs index d63abdc356234..891afbf437f2b 100644 --- a/src/librustc_mir/interpret/intrinsics.rs +++ b/src/librustc_mir/interpret/intrinsics.rs @@ -203,7 +203,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { if is_add { // max unsigned Scalar::from_uint( - u128::max_value() >> (128 - num_bits), + u128::MAX >> (128 - num_bits), Size::from_bits(num_bits), ) } else { @@ -381,11 +381,11 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { dest: PlaceTy<'tcx, M::PointerTag>, ) -> InterpResult<'tcx> { // Performs an exact division, resulting in undefined behavior where - // `x % y != 0` or `y == 0` or `x == T::min_value() && y == -1`. + // `x % y != 0` or `y == 0` or `x == T::MIN && y == -1`. // First, check x % y != 0 (or if that computation overflows). let (res, overflow, _ty) = self.overflowing_binary_op(BinOp::Rem, a, b)?; if overflow || res.assert_bits(a.layout.size) != 0 { - // Then, check if `b` is -1, which is the "min_value / -1" case. + // Then, check if `b` is -1, which is the "MIN / -1" case. let minus1 = Scalar::from_int(-1, dest.layout.size); let b_scalar = b.to_scalar().unwrap(); if b_scalar == minus1 { diff --git a/src/librustc_mir/interpret/validity.rs b/src/librustc_mir/interpret/validity.rs index 731dcc6a25f14..4f99bfe8a852a 100644 --- a/src/librustc_mir/interpret/validity.rs +++ b/src/librustc_mir/interpret/validity.rs @@ -463,7 +463,7 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, 'tcx, M let (lo, hi) = valid_range.clone().into_inner(); // Determine the allowed range // `max_hi` is as big as the size fits - let max_hi = u128::max_value() >> (128 - op.layout.size.bits()); + let max_hi = u128::MAX >> (128 - op.layout.size.bits()); assert!(hi <= max_hi); // We could also write `(hi + 1) % (max_hi + 1) == lo` but `max_hi + 1` overflows for `u128` if (lo == 0 && hi == max_hi) || (hi + 1 == lo) { From f0c3cf2b1b7bc3f6a81f91d7552f54dab667bc26 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 4 Mar 2020 13:18:08 +0100 Subject: [PATCH 4/6] cover some more nearby cases --- src/librustc/dep_graph/graph.rs | 2 +- src/librustc/ty/layout.rs | 5 ++--- src/librustc/ty/print/pretty.rs | 2 +- src/librustc/ty/query/on_disk_cache.rs | 2 +- src/librustc/ty/util.rs | 6 +++--- src/librustc_mir/dataflow/mod.rs | 1 - 6 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/librustc/dep_graph/graph.rs b/src/librustc/dep_graph/graph.rs index 33902fe913a9c..d5cde7034116c 100644 --- a/src/librustc/dep_graph/graph.rs +++ b/src/librustc/dep_graph/graph.rs @@ -524,7 +524,7 @@ impl DepGraph { edge_list_indices.push((start, end)); } - debug_assert!(edge_list_data.len() <= ::std::u32::MAX as usize); + debug_assert!(edge_list_data.len() <= u32::MAX as usize); debug_assert_eq!(edge_list_data.len(), total_edge_count); SerializedDepGraph { nodes, fingerprints, edge_list_indices, edge_list_data } diff --git a/src/librustc/ty/layout.rs b/src/librustc/ty/layout.rs index f1b4b828c71fa..7a5a417919d50 100644 --- a/src/librustc/ty/layout.rs +++ b/src/librustc/ty/layout.rs @@ -7,7 +7,6 @@ use rustc_span::DUMMY_SP; use std::cmp; use std::fmt; -use std::i128; use std::iter; use std::mem; use std::ops::Bound; @@ -1001,7 +1000,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> { } } - let (mut min, mut max) = (i128::max_value(), i128::min_value()); + let (mut min, mut max) = (i128::MAX, i128::MIN); let discr_type = def.repr.discr_type(); let bits = Integer::from_attr(self, discr_type).size().bits(); for (i, discr) in def.discriminants(tcx) { @@ -1021,7 +1020,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> { } } // We might have no inhabited variants, so pretend there's at least one. - if (min, max) == (i128::max_value(), i128::min_value()) { + if (min, max) == (i128::MAX, i128::MIN) { min = 0; max = 0; } diff --git a/src/librustc/ty/print/pretty.rs b/src/librustc/ty/print/pretty.rs index 3bf92552c8624..4829955cb70c4 100644 --- a/src/librustc/ty/print/pretty.rs +++ b/src/librustc/ty/print/pretty.rs @@ -920,7 +920,7 @@ pub trait PrettyPrinter<'tcx>: } (ConstValue::Scalar(Scalar::Raw { data, .. }), ty::Uint(ui)) => { let bit_size = Integer::from_attr(&self.tcx(), UnsignedInt(*ui)).size(); - let max = truncate(u128::max_value(), bit_size); + let max = truncate(u128::MAX, bit_size); let ui_str = ui.name_str(); if data == max { diff --git a/src/librustc/ty/query/on_disk_cache.rs b/src/librustc/ty/query/on_disk_cache.rs index 109ac97fe6265..5e36e4df698ed 100644 --- a/src/librustc/ty/query/on_disk_cache.rs +++ b/src/librustc/ty/query/on_disk_cache.rs @@ -92,7 +92,7 @@ struct AbsoluteBytePos(u32); impl AbsoluteBytePos { fn new(pos: usize) -> AbsoluteBytePos { - debug_assert!(pos <= ::std::u32::MAX as usize); + debug_assert!(pos <= u32::MAX as usize); AbsoluteBytePos(pos as u32) } diff --git a/src/librustc/ty/util.rs b/src/librustc/ty/util.rs index 7b1f821877baa..62d2b4ae20397 100644 --- a/src/librustc/ty/util.rs +++ b/src/librustc/ty/util.rs @@ -50,11 +50,11 @@ fn signed_min(size: Size) -> i128 { } fn signed_max(size: Size) -> i128 { - i128::max_value() >> (128 - size.bits()) + i128::MAX >> (128 - size.bits()) } fn unsigned_max(size: Size) -> u128 { - u128::max_value() >> (128 - size.bits()) + u128::MAX >> (128 - size.bits()) } fn int_size_and_signed<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> (Size, bool) { @@ -77,7 +77,7 @@ impl<'tcx> Discr<'tcx> { let min = signed_min(size); let max = signed_max(size); let val = sign_extend(self.val, size) as i128; - assert!(n < (i128::max_value() as u128)); + assert!(n < (i128::MAX as u128)); let n = n as i128; let oflo = val > max - n; let val = if oflo { min + (n - (max - val) - 1) } else { val + n }; diff --git a/src/librustc_mir/dataflow/mod.rs b/src/librustc_mir/dataflow/mod.rs index 25463f31867df..b4e33b9502e69 100644 --- a/src/librustc_mir/dataflow/mod.rs +++ b/src/librustc_mir/dataflow/mod.rs @@ -16,7 +16,6 @@ use std::borrow::Borrow; use std::fmt; use std::io; use std::path::PathBuf; -use std::usize; pub use self::at_location::{FlowAtLocation, FlowsAtLocation}; pub(crate) use self::drop_flag_effects::*; From 68046642c460dfd8b8042f37bd392b15f4e158e6 Mon Sep 17 00:00:00 2001 From: Ana-Maria Mihalache Date: Wed, 4 Mar 2020 14:13:00 +0000 Subject: [PATCH 5/6] Rename LayoutDetails to just Layout. --- src/librustc/arena.rs | 2 +- src/librustc/query/mod.rs | 2 +- src/librustc/ty/context.rs | 6 +- src/librustc/ty/layout.rs | 85 +++++++++---------- src/librustc_mir/interpret/operand.rs | 2 +- src/librustc_mir/interpret/place.rs | 4 +- src/librustc_mir/transform/const_prop.rs | 2 +- .../transform/uninhabited_enum_branching.rs | 2 +- src/librustc_target/abi/mod.rs | 23 ++--- src/librustc_typeck/coherence/builtin.rs | 2 +- 10 files changed, 62 insertions(+), 68 deletions(-) diff --git a/src/librustc/arena.rs b/src/librustc/arena.rs index 0384a9f781578..da0f63c0f99cd 100644 --- a/src/librustc/arena.rs +++ b/src/librustc/arena.rs @@ -19,7 +19,7 @@ use std::slice; macro_rules! arena_types { ($macro:path, $args:tt, $tcx:lifetime) => ( $macro!($args, [ - [] layouts: rustc::ty::layout::LayoutDetails, + [] layouts: rustc::ty::layout::Layout, [] generics: rustc::ty::Generics, [] trait_def: rustc::ty::TraitDef, [] adt_def: rustc::ty::AdtDef, diff --git a/src/librustc/query/mod.rs b/src/librustc/query/mod.rs index 9277f5a380b40..c08c28adbb104 100644 --- a/src/librustc/query/mod.rs +++ b/src/librustc/query/mod.rs @@ -711,7 +711,7 @@ rustc_queries! { query layout_raw( env: ty::ParamEnvAnd<'tcx, Ty<'tcx>> - ) -> Result<&'tcx ty::layout::LayoutDetails, ty::layout::LayoutError<'tcx>> { + ) -> Result<&'tcx ty::layout::Layout, ty::layout::LayoutError<'tcx>> { no_force desc { "computing layout of `{}`", env.value } } diff --git a/src/librustc/ty/context.rs b/src/librustc/ty/context.rs index 20736b50831bb..51cc77df9a5a3 100644 --- a/src/librustc/ty/context.rs +++ b/src/librustc/ty/context.rs @@ -23,7 +23,7 @@ use crate::mir::{ use crate::traits; use crate::traits::{Clause, Clauses, Goal, GoalKind, Goals}; use crate::ty::free_region_map::FreeRegionMap; -use crate::ty::layout::{LayoutDetails, TargetDataLayout, VariantIdx}; +use crate::ty::layout::{Layout, TargetDataLayout, VariantIdx}; use crate::ty::query; use crate::ty::steal::Steal; use crate::ty::subst::{GenericArg, InternalSubsts, Subst, SubstsRef}; @@ -1019,7 +1019,7 @@ pub struct GlobalCtxt<'tcx> { pub alloc_map: Lock>, - layout_interner: ShardedHashMap<&'tcx LayoutDetails, ()>, + layout_interner: ShardedHashMap<&'tcx Layout, ()>, output_filenames: Arc, } @@ -1074,7 +1074,7 @@ impl<'tcx> TyCtxt<'tcx> { self.const_stability_interner.intern(stab, |stab| self.arena.alloc(stab)) } - pub fn intern_layout(self, layout: LayoutDetails) -> &'tcx LayoutDetails { + pub fn intern_layout(self, layout: Layout) -> &'tcx Layout { self.layout_interner.intern(layout, |layout| self.arena.alloc(layout)) } diff --git a/src/librustc/ty/layout.rs b/src/librustc/ty/layout.rs index f1b4b828c71fa..d5ca9fa65b694 100644 --- a/src/librustc/ty/layout.rs +++ b/src/librustc/ty/layout.rs @@ -184,7 +184,7 @@ impl<'tcx> fmt::Display for LayoutError<'tcx> { fn layout_raw<'tcx>( tcx: TyCtxt<'tcx>, query: ty::ParamEnvAnd<'tcx, Ty<'tcx>>, -) -> Result<&'tcx LayoutDetails, LayoutError<'tcx>> { +) -> Result<&'tcx Layout, LayoutError<'tcx>> { ty::tls::with_related_context(tcx, move |icx| { let rec_limit = *tcx.sess.recursion_limit.get(); let (param_env, ty) = query.into_parts(); @@ -243,7 +243,7 @@ fn invert_mapping(map: &[u32]) -> Vec { } impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> { - fn scalar_pair(&self, a: Scalar, b: Scalar) -> LayoutDetails { + fn scalar_pair(&self, a: Scalar, b: Scalar) -> Layout { let dl = self.data_layout(); let b_align = b.value.align(dl); let align = a.value.align(dl).max(b_align).max(dl.aggregate_align); @@ -257,7 +257,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> { .chain(Niche::from_scalar(dl, Size::ZERO, a.clone())) .max_by_key(|niche| niche.available(dl)); - LayoutDetails { + Layout { variants: Variants::Single { index: VariantIdx::new(0) }, fields: FieldPlacement::Arbitrary { offsets: vec![Size::ZERO, b_offset], @@ -276,7 +276,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> { fields: &[TyLayout<'_>], repr: &ReprOptions, kind: StructKind, - ) -> Result> { + ) -> Result> { let dl = self.data_layout(); let pack = repr.pack; if pack.is_some() && repr.align.is_some() { @@ -429,17 +429,11 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> { ( Some(( i, - &TyLayout { - details: &LayoutDetails { abi: Abi::Scalar(ref a), .. }, - .. - }, + &TyLayout { layout: &Layout { abi: Abi::Scalar(ref a), .. }, .. }, )), Some(( j, - &TyLayout { - details: &LayoutDetails { abi: Abi::Scalar(ref b), .. }, - .. - }, + &TyLayout { layout: &Layout { abi: Abi::Scalar(ref b), .. }, .. }, )), None, ) => { @@ -477,7 +471,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> { abi = Abi::Uninhabited; } - Ok(LayoutDetails { + Ok(Layout { variants: Variants::Single { index: VariantIdx::new(0) }, fields: FieldPlacement::Arbitrary { offsets, memory_index }, abi, @@ -487,7 +481,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> { }) } - fn layout_raw_uncached(&self, ty: Ty<'tcx>) -> Result<&'tcx LayoutDetails, LayoutError<'tcx>> { + fn layout_raw_uncached(&self, ty: Ty<'tcx>) -> Result<&'tcx Layout, LayoutError<'tcx>> { let tcx = self.tcx; let param_env = self.param_env; let dl = self.data_layout(); @@ -496,8 +490,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> { assert!(bits <= 128); Scalar { value, valid_range: 0..=(!0 >> (128 - bits)) } }; - let scalar = - |value: Primitive| tcx.intern_layout(LayoutDetails::scalar(self, scalar_unit(value))); + let scalar = |value: Primitive| tcx.intern_layout(Layout::scalar(self, scalar_unit(value))); let univariant = |fields: &[TyLayout<'_>], repr: &ReprOptions, kind| { Ok(tcx.intern_layout(self.univariant_uninterned(ty, fields, repr, kind)?)) @@ -506,11 +499,11 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> { Ok(match ty.kind { // Basic scalars. - ty::Bool => tcx.intern_layout(LayoutDetails::scalar( + ty::Bool => tcx.intern_layout(Layout::scalar( self, Scalar { value: Int(I8, false), valid_range: 0..=1 }, )), - ty::Char => tcx.intern_layout(LayoutDetails::scalar( + ty::Char => tcx.intern_layout(Layout::scalar( self, Scalar { value: Int(I32, false), valid_range: 0..=0x10FFFF }, )), @@ -523,11 +516,11 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> { ty::FnPtr(_) => { let mut ptr = scalar_unit(Pointer); ptr.valid_range = 1..=*ptr.valid_range.end(); - tcx.intern_layout(LayoutDetails::scalar(self, ptr)) + tcx.intern_layout(Layout::scalar(self, ptr)) } // The never type. - ty::Never => tcx.intern_layout(LayoutDetails { + ty::Never => tcx.intern_layout(Layout { variants: Variants::Single { index: VariantIdx::new(0) }, fields: FieldPlacement::Union(0), abi: Abi::Uninhabited, @@ -545,13 +538,13 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> { let pointee = tcx.normalize_erasing_regions(param_env, pointee); if pointee.is_sized(tcx.at(DUMMY_SP), param_env) { - return Ok(tcx.intern_layout(LayoutDetails::scalar(self, data_ptr))); + return Ok(tcx.intern_layout(Layout::scalar(self, data_ptr))); } let unsized_part = tcx.struct_tail_erasing_lifetimes(pointee, param_env); let metadata = match unsized_part.kind { ty::Foreign(..) => { - return Ok(tcx.intern_layout(LayoutDetails::scalar(self, data_ptr))); + return Ok(tcx.intern_layout(Layout::scalar(self, data_ptr))); } ty::Slice(_) | ty::Str => scalar_unit(Int(dl.ptr_sized_integer(), false)), ty::Dynamic(..) => { @@ -588,7 +581,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> { let largest_niche = if count != 0 { element.largest_niche.clone() } else { None }; - tcx.intern_layout(LayoutDetails { + tcx.intern_layout(Layout { variants: Variants::Single { index: VariantIdx::new(0) }, fields: FieldPlacement::Array { stride: element.size, count }, abi, @@ -599,7 +592,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> { } ty::Slice(element) => { let element = self.layout_of(element)?; - tcx.intern_layout(LayoutDetails { + tcx.intern_layout(Layout { variants: Variants::Single { index: VariantIdx::new(0) }, fields: FieldPlacement::Array { stride: element.size, count: 0 }, abi: Abi::Aggregate { sized: false }, @@ -608,7 +601,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> { size: Size::ZERO, }) } - ty::Str => tcx.intern_layout(LayoutDetails { + ty::Str => tcx.intern_layout(Layout { variants: Variants::Single { index: VariantIdx::new(0) }, fields: FieldPlacement::Array { stride: Size::from_bytes(1), count: 0 }, abi: Abi::Aggregate { sized: false }, @@ -677,7 +670,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> { let align = dl.vector_align(size); let size = size.align_to(align.abi); - tcx.intern_layout(LayoutDetails { + tcx.intern_layout(Layout { variants: Variants::Single { index: VariantIdx::new(0) }, fields: FieldPlacement::Array { stride: element.size, count }, abi: Abi::Vector { element: scalar, count }, @@ -753,7 +746,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> { align = align.min(AbiAndPrefAlign::new(pack)); } - return Ok(tcx.intern_layout(LayoutDetails { + return Ok(tcx.intern_layout(Layout { variants: Variants::Single { index }, fields: FieldPlacement::Union(variants[index].len()), abi, @@ -977,7 +970,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> { let largest_niche = Niche::from_scalar(dl, offset, niche_scalar.clone()); - return Ok(tcx.intern_layout(LayoutDetails { + return Ok(tcx.intern_layout(Layout { variants: Variants::Multiple { discr: niche_scalar, discr_kind: DiscriminantKind::Niche { @@ -1172,7 +1165,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> { break; } }; - let prim = match field.details.abi { + let prim = match field.abi { Abi::Scalar(ref scalar) => scalar.value, _ => { common_prim = None; @@ -1219,7 +1212,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> { let largest_niche = Niche::from_scalar(dl, Size::ZERO, tag.clone()); - tcx.intern_layout(LayoutDetails { + tcx.intern_layout(Layout { variants: Variants::Multiple { discr: tag, discr_kind: DiscriminantKind::Tag, @@ -1250,7 +1243,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> { | ty::Placeholder(..) | ty::UnnormalizedProjection(..) | ty::GeneratorWitness(..) - | ty::Infer(_) => bug!("LayoutDetails::compute: unexpected type `{}`", ty), + | ty::Infer(_) => bug!("Layout::compute: unexpected type `{}`", ty), ty::Param(_) | ty::Error => { return Err(LayoutError::Unknown(ty)); @@ -1397,7 +1390,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> { ty: Ty<'tcx>, def_id: hir::def_id::DefId, substs: SubstsRef<'tcx>, - ) -> Result<&'tcx LayoutDetails, LayoutError<'tcx>> { + ) -> Result<&'tcx Layout, LayoutError<'tcx>> { use SavedLocalEligibility::*; let tcx = self.tcx; @@ -1563,7 +1556,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> { Abi::Aggregate { sized: true } }; - let layout = tcx.intern_layout(LayoutDetails { + let layout = tcx.intern_layout(Layout { variants: Variants::Multiple { discr, discr_kind: DiscriminantKind::Tag, @@ -1946,8 +1939,8 @@ impl<'tcx> LayoutOf for LayoutCx<'tcx, TyCtxt<'tcx>> { fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyLayout { let param_env = self.param_env.with_reveal_all(); let ty = self.tcx.normalize_erasing_regions(param_env, ty); - let details = self.tcx.layout_raw(param_env.and(ty))?; - let layout = TyLayout { ty, details }; + let layout = self.tcx.layout_raw(param_env.and(ty))?; + let layout = TyLayout { ty, layout }; // N.B., this recording is normally disabled; when enabled, it // can however trigger recursive invocations of `layout_of`. @@ -1970,8 +1963,8 @@ impl LayoutOf for LayoutCx<'tcx, ty::query::TyCtxtAt<'tcx>> { fn layout_of(&self, ty: Ty<'tcx>) -> Self::TyLayout { let param_env = self.param_env.with_reveal_all(); let ty = self.tcx.normalize_erasing_regions(param_env, ty); - let details = self.tcx.layout_raw(param_env.and(ty))?; - let layout = TyLayout { ty, details }; + let layout = self.tcx.layout_raw(param_env.and(ty))?; + let layout = TyLayout { ty, layout }; // N.B., this recording is normally disabled; when enabled, it // can however trigger recursive invocations of `layout_of`. @@ -2020,13 +2013,13 @@ where + HasParamEnv<'tcx>, { fn for_variant(this: TyLayout<'tcx>, cx: &C, variant_index: VariantIdx) -> TyLayout<'tcx> { - let details = match this.variants { - Variants::Single { index } if index == variant_index => this.details, + let layout = match this.variants { + Variants::Single { index } if index == variant_index => this.layout, Variants::Single { index } => { // Deny calling for_variant more than once for non-Single enums. - if let Ok(layout) = cx.layout_of(this.ty).to_result() { - assert_eq!(layout.variants, Variants::Single { index }); + if let Ok(original_layout) = cx.layout_of(this.ty).to_result() { + assert_eq!(original_layout.variants, Variants::Single { index }); } let fields = match this.ty.kind { @@ -2034,7 +2027,7 @@ where _ => bug!(), }; let tcx = cx.tcx(); - tcx.intern_layout(LayoutDetails { + tcx.intern_layout(Layout { variants: Variants::Single { index: variant_index }, fields: FieldPlacement::Union(fields), abi: Abi::Uninhabited, @@ -2047,17 +2040,17 @@ where Variants::Multiple { ref variants, .. } => &variants[variant_index], }; - assert_eq!(details.variants, Variants::Single { index: variant_index }); + assert_eq!(layout.variants, Variants::Single { index: variant_index }); - TyLayout { ty: this.ty, details } + TyLayout { ty: this.ty, layout } } fn field(this: TyLayout<'tcx>, cx: &C, i: usize) -> C::TyLayout { let tcx = cx.tcx(); let discr_layout = |discr: &Scalar| -> C::TyLayout { - let layout = LayoutDetails::scalar(cx, discr.clone()); + let layout = Layout::scalar(cx, discr.clone()); MaybeResult::from(Ok(TyLayout { - details: tcx.intern_layout(layout), + layout: tcx.intern_layout(layout), ty: discr.value.to_ty(tcx), })) }; diff --git a/src/librustc_mir/interpret/operand.rs b/src/librustc_mir/interpret/operand.rs index 44b46d65bf1c7..f25691290e68c 100644 --- a/src/librustc_mir/interpret/operand.rs +++ b/src/librustc_mir/interpret/operand.rs @@ -220,7 +220,7 @@ pub(super) fn from_known_layout<'tcx>( if cfg!(debug_assertions) { let layout2 = compute()?; assert_eq!( - layout.details, layout2.details, + layout.layout, layout2.layout, "mismatch in layout of supposedly equal-layout types {:?} and {:?}", layout.ty, layout2.ty ); diff --git a/src/librustc_mir/interpret/place.rs b/src/librustc_mir/interpret/place.rs index 4f96cb698915d..80ea108b48b20 100644 --- a/src/librustc_mir/interpret/place.rs +++ b/src/librustc_mir/interpret/place.rs @@ -853,7 +853,7 @@ where // We do NOT compare the types for equality, because well-typed code can // actually "transmute" `&mut T` to `&T` in an assignment without a cast. assert!( - src.layout.details == dest.layout.details, + src.layout.layout == dest.layout.layout, "Layout mismatch when copying!\nsrc: {:#?}\ndest: {:#?}", src, dest @@ -907,7 +907,7 @@ where src: OpTy<'tcx, M::PointerTag>, dest: PlaceTy<'tcx, M::PointerTag>, ) -> InterpResult<'tcx> { - if src.layout.details == dest.layout.details { + if src.layout.layout == dest.layout.layout { // Fast path: Just use normal `copy_op` return self.copy_op(src, dest); } diff --git a/src/librustc_mir/transform/const_prop.rs b/src/librustc_mir/transform/const_prop.rs index c196e748df381..9ccc4a01d17e5 100644 --- a/src/librustc_mir/transform/const_prop.rs +++ b/src/librustc_mir/transform/const_prop.rs @@ -692,7 +692,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> { let ty1 = substs[0].expect_ty(); let ty2 = substs[1].expect_ty(); let ty_is_scalar = |ty| { - this.ecx.layout_of(ty).ok().map(|ty| ty.details.abi.is_scalar()) + this.ecx.layout_of(ty).ok().map(|layout| layout.abi.is_scalar()) == Some(true) }; if ty_is_scalar(ty1) && ty_is_scalar(ty2) { diff --git a/src/librustc_mir/transform/uninhabited_enum_branching.rs b/src/librustc_mir/transform/uninhabited_enum_branching.rs index bbaa66f5954cb..7178e614f0a97 100644 --- a/src/librustc_mir/transform/uninhabited_enum_branching.rs +++ b/src/librustc_mir/transform/uninhabited_enum_branching.rs @@ -53,7 +53,7 @@ fn variant_discriminants<'tcx>( ty: Ty<'tcx>, tcx: TyCtxt<'tcx>, ) -> Vec { - match &layout.details.variants { + match &layout.variants { Variants::Single { index } => vec![index.as_u32() as u128], Variants::Multiple { variants, .. } => variants .iter_enumerated() diff --git a/src/librustc_target/abi/mod.rs b/src/librustc_target/abi/mod.rs index ffef38cedfc17..eaa91533517c7 100644 --- a/src/librustc_target/abi/mod.rs +++ b/src/librustc_target/abi/mod.rs @@ -787,7 +787,7 @@ pub enum Variants { discr: Scalar, discr_kind: DiscriminantKind, discr_index: usize, - variants: IndexVec, + variants: IndexVec, }, } @@ -870,7 +870,7 @@ impl Niche { } #[derive(PartialEq, Eq, Hash, Debug, HashStable_Generic)] -pub struct LayoutDetails { +pub struct Layout { /// Says where the fields are located within the layout. /// Primitives and fieldless enums appear as unions without fields. pub fields: FieldPlacement, @@ -901,12 +901,12 @@ pub struct LayoutDetails { pub size: Size, } -impl LayoutDetails { +impl Layout { pub fn scalar(cx: &C, scalar: Scalar) -> Self { let largest_niche = Niche::from_scalar(cx, Size::ZERO, scalar.clone()); let size = scalar.value.size(cx); let align = scalar.value.align(cx); - LayoutDetails { + Layout { variants: Variants::Single { index: VariantIdx::new(0) }, fields: FieldPlacement::Union(0), abi: Abi::Scalar(scalar), @@ -917,23 +917,24 @@ impl LayoutDetails { } } -/// The details of the layout of a type, alongside the type itself. +/// The layout of a type, alongside the type itself. /// Provides various type traversal APIs (e.g., recursing into fields). /// -/// Note that the details are NOT guaranteed to always be identical -/// to those obtained from `layout_of(ty)`, as we need to produce +/// Note that the layout is NOT guaranteed to always be identical +/// to that obtained from `layout_of(ty)`, as we need to produce /// layouts for which Rust types do not exist, such as enum variants /// or synthetic fields of enums (i.e., discriminants) and fat pointers. +// FIXME: rename to TyAndLayout. #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] pub struct TyLayout<'a, Ty> { pub ty: Ty, - pub details: &'a LayoutDetails, + pub layout: &'a Layout, } impl<'a, Ty> Deref for TyLayout<'a, Ty> { - type Target = &'a LayoutDetails; - fn deref(&self) -> &&'a LayoutDetails { - &self.details + type Target = &'a Layout; + fn deref(&self) -> &&'a Layout { + &self.layout } } diff --git a/src/librustc_typeck/coherence/builtin.rs b/src/librustc_typeck/coherence/builtin.rs index aa39a191b3df6..f061dad5a9d10 100644 --- a/src/librustc_typeck/coherence/builtin.rs +++ b/src/librustc_typeck/coherence/builtin.rs @@ -217,7 +217,7 @@ fn visit_implementation_of_dispatch_from_dyn(tcx: TyCtxt<'_>, impl_did: DefId) { let ty_b = field.ty(tcx, substs_b); if let Ok(layout) = tcx.layout_of(param_env.and(ty_a)) { - if layout.is_zst() && layout.details.align.abi.bytes() == 1 { + if layout.is_zst() && layout.align.abi.bytes() == 1 { // ignore ZST fields with alignment of 1 byte return None; } From 8ea676eccaa3596c56b5bf665a1adb155b634ff3 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Mon, 2 Mar 2020 10:00:05 -0800 Subject: [PATCH 6/6] Update books --- src/doc/embedded-book | 2 +- src/doc/nomicon | 2 +- src/doc/reference | 2 +- src/doc/rust-by-example | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/doc/embedded-book b/src/doc/embedded-book index b2e1092bf67bd..b81ffb7a6f4c5 160000 --- a/src/doc/embedded-book +++ b/src/doc/embedded-book @@ -1 +1 @@ -Subproject commit b2e1092bf67bd4d7686c4553f186edbb7f5f92db +Subproject commit b81ffb7a6f4c5aaed92786e770e99db116aa4ebd diff --git a/src/doc/nomicon b/src/doc/nomicon index 3e6e1001dc6e0..9f797e65e6bcc 160000 --- a/src/doc/nomicon +++ b/src/doc/nomicon @@ -1 +1 @@ -Subproject commit 3e6e1001dc6e095dbd5c88005e80969f60e384e1 +Subproject commit 9f797e65e6bcc79419975b17aff8e21c9adc039f diff --git a/src/doc/reference b/src/doc/reference index 64239df6d1735..559e09caa9661 160000 --- a/src/doc/reference +++ b/src/doc/reference @@ -1 +1 @@ -Subproject commit 64239df6d173562b9deb4f012e4c3e6e960c4754 +Subproject commit 559e09caa9661043744cf7af7bd88432d966f743 diff --git a/src/doc/rust-by-example b/src/doc/rust-by-example index 32facd5522ddb..db57f899ea2a5 160000 --- a/src/doc/rust-by-example +++ b/src/doc/rust-by-example @@ -1 +1 @@ -Subproject commit 32facd5522ddbbf37baf01e4e4b6562bc55c071a +Subproject commit db57f899ea2a56a544c8d280cbf033438666273d