Skip to content

Commit 53b352e

Browse files
committed
Auto merge of #64616 - Centril:rollup-du6728f, r=Centril
Rollup of 6 pull requests Successful merges: - #63448 (fix Miri discriminant handling) - #64592 (Point at original span when emitting unreachable lint) - #64601 (Fix backticks in documentation) - #64606 (Remove unnecessary `mut` in doc example) - #64611 (rustbuild: Don't package libstd twice) - #64613 (rustbuild: Copy crate doc files fewer times) Failed merges: r? @ghost
2 parents 9b9d2af + 99cbffb commit 53b352e

Some content is hidden

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

49 files changed

+633
-49
lines changed

src/bootstrap/dist.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ impl Step for Analysis {
762762
return distdir(builder).join(format!("{}-{}.tar.gz", name, target));
763763
}
764764

765-
builder.ensure(Std { compiler, target });
765+
builder.ensure(compile::Std { compiler, target });
766766

767767
let image = tmpdir(builder).join(format!("{}-{}-image", name, target));
768768

src/bootstrap/doc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -476,11 +476,11 @@ impl Step for Std {
476476
.arg("--index-page").arg(&builder.src.join("src/doc/index.md"));
477477

478478
builder.run(&mut cargo);
479-
builder.cp_r(&my_out, &out);
480479
};
481480
for krate in &["alloc", "core", "std", "proc_macro", "test"] {
482481
run_cargo_rustdoc_for(krate);
483482
}
483+
builder.cp_r(&my_out, &out);
484484
}
485485
}
486486

src/librustc/middle/region.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1136,7 +1136,7 @@ fn resolve_local<'tcx>(
11361136
// Rule A. `let (ref x, ref y) = (foo().x, 44)`. The rvalue `(22, 44)`
11371137
// would have an extended lifetime, but not `foo()`.
11381138
//
1139-
// Rule B. `let x = &foo().x`. The rvalue ``foo()` would have extended
1139+
// Rule B. `let x = &foo().x`. The rvalue `foo()` would have extended
11401140
// lifetime.
11411141
//
11421142
// In some cases, multiple rules may apply (though not to the same

src/librustc/ty/layout.rs

+11
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ impl IntegerExt for Integer {
127127

128128
pub trait PrimitiveExt {
129129
fn to_ty<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx>;
130+
fn to_int_ty<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx>;
130131
}
131132

132133
impl PrimitiveExt for Primitive {
@@ -138,6 +139,16 @@ impl PrimitiveExt for Primitive {
138139
Pointer => tcx.mk_mut_ptr(tcx.mk_unit()),
139140
}
140141
}
142+
143+
/// Return an *integer* type matching this primitive.
144+
/// Useful in particular when dealing with enum discriminants.
145+
fn to_int_ty(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
146+
match *self {
147+
Int(i, signed) => i.to_ty(tcx, signed),
148+
Pointer => tcx.types.usize,
149+
Float(..) => bug!("floats do not have an int type"),
150+
}
151+
}
141152
}
142153

143154
/// The first half of a fat pointer.

src/librustc/ty/query/on_disk_cache.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ impl<'a, 'tcx> SpecializedDecoder<DefIndex> for CacheDecoder<'a, 'tcx> {
643643

644644
// Both the `CrateNum` and the `DefIndex` of a `DefId` can change in between two
645645
// compilation sessions. We use the `DefPathHash`, which is stable across
646-
// sessions, to map the old DefId`` to the new one.
646+
// sessions, to map the old `DefId` to the new one.
647647
impl<'a, 'tcx> SpecializedDecoder<DefId> for CacheDecoder<'a, 'tcx> {
648648
#[inline]
649649
fn specialized_decode(&mut self) -> Result<DefId, Self::Error> {

src/librustc_mir/interpret/operand.rs

+37-16
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
//! Functions concerning immediate values and operands, and reading from operands.
22
//! All high-level functions to read from memory work on operands as sources.
33
4-
use std::convert::TryInto;
4+
use std::convert::{TryInto, TryFrom};
55

66
use rustc::{mir, ty};
77
use rustc::ty::layout::{
8-
self, Size, LayoutOf, TyLayout, HasDataLayout, IntegerExt, VariantIdx,
8+
self, Size, LayoutOf, TyLayout, HasDataLayout, IntegerExt, PrimitiveExt, VariantIdx,
99
};
1010

1111
use rustc::mir::interpret::{
@@ -609,15 +609,20 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
609609
) -> InterpResult<'tcx, (u128, VariantIdx)> {
610610
trace!("read_discriminant_value {:#?}", rval.layout);
611611

612-
let (discr_kind, discr_index) = match rval.layout.variants {
612+
let (discr_layout, discr_kind, discr_index) = match rval.layout.variants {
613613
layout::Variants::Single { index } => {
614614
let discr_val = rval.layout.ty.discriminant_for_variant(*self.tcx, index).map_or(
615615
index.as_u32() as u128,
616616
|discr| discr.val);
617617
return Ok((discr_val, index));
618618
}
619-
layout::Variants::Multiple { ref discr_kind, discr_index, .. } =>
620-
(discr_kind, discr_index),
619+
layout::Variants::Multiple {
620+
discr: ref discr_layout,
621+
ref discr_kind,
622+
discr_index,
623+
..
624+
} =>
625+
(discr_layout, discr_kind, discr_index),
621626
};
622627

623628
// read raw discriminant value
@@ -634,7 +639,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
634639
.map_err(|_| err_unsup!(InvalidDiscriminant(raw_discr.erase_tag())))?;
635640
let real_discr = if discr_val.layout.ty.is_signed() {
636641
// going from layout tag type to typeck discriminant type
637-
// requires first sign extending with the layout discriminant
642+
// requires first sign extending with the discriminant layout
638643
let sexted = sign_extend(bits_discr, discr_val.layout.size) as i128;
639644
// and then zeroing with the typeck discriminant type
640645
let discr_ty = rval.layout.ty
@@ -666,8 +671,8 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
666671
ref niche_variants,
667672
niche_start,
668673
} => {
669-
let variants_start = niche_variants.start().as_u32() as u128;
670-
let variants_end = niche_variants.end().as_u32() as u128;
674+
let variants_start = niche_variants.start().as_u32();
675+
let variants_end = niche_variants.end().as_u32();
671676
let raw_discr = raw_discr.not_undef().map_err(|_| {
672677
err_unsup!(InvalidDiscriminant(ScalarMaybeUndef::Undef))
673678
})?;
@@ -682,18 +687,34 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
682687
(dataful_variant.as_u32() as u128, dataful_variant)
683688
},
684689
Ok(raw_discr) => {
685-
let adjusted_discr = raw_discr.wrapping_sub(niche_start)
686-
.wrapping_add(variants_start);
687-
if variants_start <= adjusted_discr && adjusted_discr <= variants_end {
688-
let index = adjusted_discr as usize;
689-
assert_eq!(index as u128, adjusted_discr);
690-
assert!(index < rval.layout.ty
690+
// We need to use machine arithmetic to get the relative variant idx:
691+
// variant_index_relative = discr_val - niche_start_val
692+
let discr_layout = self.layout_of(discr_layout.value.to_int_ty(*self.tcx))?;
693+
let discr_val = ImmTy::from_uint(raw_discr, discr_layout);
694+
let niche_start_val = ImmTy::from_uint(niche_start, discr_layout);
695+
let variant_index_relative_val = self.binary_op(
696+
mir::BinOp::Sub,
697+
discr_val,
698+
niche_start_val,
699+
)?;
700+
let variant_index_relative = variant_index_relative_val
701+
.to_scalar()?
702+
.assert_bits(discr_val.layout.size);
703+
// Check if this is in the range that indicates an actual discriminant.
704+
if variant_index_relative <= u128::from(variants_end - variants_start) {
705+
let variant_index_relative = u32::try_from(variant_index_relative)
706+
.expect("we checked that this fits into a u32");
707+
// Then computing the absolute variant idx should not overflow any more.
708+
let variant_index = variants_start
709+
.checked_add(variant_index_relative)
710+
.expect("oveflow computing absolute variant idx");
711+
assert!((variant_index as usize) < rval.layout.ty
691712
.ty_adt_def()
692713
.expect("tagged layout for non adt")
693714
.variants.len());
694-
(adjusted_discr, VariantIdx::from_usize(index))
715+
(u128::from(variant_index), VariantIdx::from_u32(variant_index))
695716
} else {
696-
(dataful_variant.as_u32() as u128, dataful_variant)
717+
(u128::from(dataful_variant.as_u32()), dataful_variant)
697718
}
698719
},
699720
}

src/librustc_mir/interpret/place.rs

+23-11
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ use std::hash::Hash;
88
use rustc::mir;
99
use rustc::mir::interpret::truncate;
1010
use rustc::ty::{self, Ty};
11-
use rustc::ty::layout::{self, Size, Align, LayoutOf, TyLayout, HasDataLayout, VariantIdx};
11+
use rustc::ty::layout::{
12+
self, Size, Align, LayoutOf, TyLayout, HasDataLayout, VariantIdx, PrimitiveExt
13+
};
1214
use rustc::ty::TypeFoldable;
1315

1416
use super::{
@@ -1027,7 +1029,7 @@ where
10271029
}
10281030
layout::Variants::Multiple {
10291031
discr_kind: layout::DiscriminantKind::Tag,
1030-
ref discr,
1032+
discr: ref discr_layout,
10311033
discr_index,
10321034
..
10331035
} => {
@@ -1038,7 +1040,7 @@ where
10381040
// raw discriminants for enums are isize or bigger during
10391041
// their computation, but the in-memory tag is the smallest possible
10401042
// representation
1041-
let size = discr.value.size(self);
1043+
let size = discr_layout.value.size(self);
10421044
let discr_val = truncate(discr_val, size);
10431045

10441046
let discr_dest = self.place_field(dest, discr_index as u64)?;
@@ -1050,22 +1052,32 @@ where
10501052
ref niche_variants,
10511053
niche_start,
10521054
},
1055+
discr: ref discr_layout,
10531056
discr_index,
10541057
..
10551058
} => {
10561059
assert!(
10571060
variant_index.as_usize() < dest.layout.ty.ty_adt_def().unwrap().variants.len(),
10581061
);
10591062
if variant_index != dataful_variant {
1060-
let niche_dest =
1061-
self.place_field(dest, discr_index as u64)?;
1062-
let niche_value = variant_index.as_u32() - niche_variants.start().as_u32();
1063-
let niche_value = (niche_value as u128)
1064-
.wrapping_add(niche_start);
1065-
self.write_scalar(
1066-
Scalar::from_uint(niche_value, niche_dest.layout.size),
1067-
niche_dest
1063+
let variants_start = niche_variants.start().as_u32();
1064+
let variant_index_relative = variant_index.as_u32()
1065+
.checked_sub(variants_start)
1066+
.expect("overflow computing relative variant idx");
1067+
// We need to use machine arithmetic when taking into account `niche_start`:
1068+
// discr_val = variant_index_relative + niche_start_val
1069+
let discr_layout = self.layout_of(discr_layout.value.to_int_ty(*self.tcx))?;
1070+
let niche_start_val = ImmTy::from_uint(niche_start, discr_layout);
1071+
let variant_index_relative_val =
1072+
ImmTy::from_uint(variant_index_relative, discr_layout);
1073+
let discr_val = self.binary_op(
1074+
mir::BinOp::Add,
1075+
variant_index_relative_val,
1076+
niche_start_val,
10681077
)?;
1078+
// Write result.
1079+
let niche_dest = self.place_field(dest, discr_index as u64)?;
1080+
self.write_immediate(*discr_val, niche_dest)?;
10691081
}
10701082
}
10711083
}

src/librustc_typeck/check/_match.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
4343

4444
// If there are no arms, that is a diverging match; a special case.
4545
if arms.is_empty() {
46-
self.diverges.set(self.diverges.get() | Diverges::Always);
46+
self.diverges.set(self.diverges.get() | Diverges::always(expr.span));
4747
return tcx.types.never;
4848
}
4949

@@ -69,7 +69,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
6969
// warnings).
7070
match all_pats_diverge {
7171
Diverges::Maybe => Diverges::Maybe,
72-
Diverges::Always | Diverges::WarnedAlways => Diverges::WarnedAlways,
72+
Diverges::Always { .. } | Diverges::WarnedAlways => Diverges::WarnedAlways,
7373
}
7474
}).collect();
7575

@@ -167,6 +167,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
167167
prior_arm_ty = Some(arm_ty);
168168
}
169169

170+
// If all of the arms in the `match` diverge,
171+
// and we're dealing with an actual `match` block
172+
// (as opposed to a `match` desugared from something else'),
173+
// we can emit a better note. Rather than pointing
174+
// at a diverging expression in an arbitrary arm,
175+
// we can point at the entire `match` expression
176+
if let (Diverges::Always { .. }, hir::MatchSource::Normal) = (all_arms_diverge, match_src) {
177+
all_arms_diverge = Diverges::Always {
178+
span: expr.span,
179+
custom_note: Some(
180+
"any code following this `match` expression is unreachable, as all arms diverge"
181+
)
182+
};
183+
}
184+
170185
// We won't diverge unless the discriminant or all arms diverge.
171186
self.diverges.set(discrim_diverges | all_arms_diverge);
172187

@@ -176,7 +191,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
176191
/// When the previously checked expression (the scrutinee) diverges,
177192
/// warn the user about the match arms being unreachable.
178193
fn warn_arms_when_scrutinee_diverges(&self, arms: &'tcx [hir::Arm], source: hir::MatchSource) {
179-
if self.diverges.get().always() {
194+
if self.diverges.get().is_always() {
180195
use hir::MatchSource::*;
181196
let msg = match source {
182197
IfDesugar { .. } | IfLetDesugar { .. } => "block in `if` expression",

src/librustc_typeck/check/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
170170

171171
// Any expression that produces a value of type `!` must have diverged
172172
if ty.is_never() {
173-
self.diverges.set(self.diverges.get() | Diverges::Always);
173+
self.diverges.set(self.diverges.get() | Diverges::always(expr.span));
174174
}
175175

176176
// Record the type, which applies it effects.

src/librustc_typeck/check/mod.rs

+45-10
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,20 @@ pub enum Diverges {
450450

451451
/// Definitely known to diverge and therefore
452452
/// not reach the next sibling or its parent.
453-
Always,
453+
Always {
454+
/// The `Span` points to the expression
455+
/// that caused us to diverge
456+
/// (e.g. `return`, `break`, etc).
457+
span: Span,
458+
/// In some cases (e.g. a `match` expression
459+
/// where all arms diverge), we may be
460+
/// able to provide a more informative
461+
/// message to the user.
462+
/// If this is `None`, a default messsage
463+
/// will be generated, which is suitable
464+
/// for most cases.
465+
custom_note: Option<&'static str>
466+
},
454467

455468
/// Same as `Always` but with a reachability
456469
/// warning already emitted.
@@ -486,8 +499,22 @@ impl ops::BitOrAssign for Diverges {
486499
}
487500

488501
impl Diverges {
489-
fn always(self) -> bool {
490-
self >= Diverges::Always
502+
/// Creates a `Diverges::Always` with the provided `span` and the default note message.
503+
fn always(span: Span) -> Diverges {
504+
Diverges::Always {
505+
span,
506+
custom_note: None
507+
}
508+
}
509+
510+
fn is_always(self) -> bool {
511+
// Enum comparison ignores the
512+
// contents of fields, so we just
513+
// fill them in with garbage here.
514+
self >= Diverges::Always {
515+
span: DUMMY_SP,
516+
custom_note: None
517+
}
491518
}
492519
}
493520

@@ -2307,17 +2334,25 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
23072334
/// Produces warning on the given node, if the current point in the
23082335
/// function is unreachable, and there hasn't been another warning.
23092336
fn warn_if_unreachable(&self, id: hir::HirId, span: Span, kind: &str) {
2310-
if self.diverges.get() == Diverges::Always &&
2337+
// FIXME: Combine these two 'if' expressions into one once
2338+
// let chains are implemented
2339+
if let Diverges::Always { span: orig_span, custom_note } = self.diverges.get() {
23112340
// If span arose from a desugaring of `if` or `while`, then it is the condition itself,
23122341
// which diverges, that we are about to lint on. This gives suboptimal diagnostics.
23132342
// Instead, stop here so that the `if`- or `while`-expression's block is linted instead.
2314-
!span.is_desugaring(DesugaringKind::CondTemporary) {
2315-
self.diverges.set(Diverges::WarnedAlways);
2343+
if !span.is_desugaring(DesugaringKind::CondTemporary) {
2344+
self.diverges.set(Diverges::WarnedAlways);
23162345

2317-
debug!("warn_if_unreachable: id={:?} span={:?} kind={}", id, span, kind);
2346+
debug!("warn_if_unreachable: id={:?} span={:?} kind={}", id, span, kind);
23182347

2319-
let msg = format!("unreachable {}", kind);
2320-
self.tcx().lint_hir(lint::builtin::UNREACHABLE_CODE, id, span, &msg);
2348+
let msg = format!("unreachable {}", kind);
2349+
self.tcx().struct_span_lint_hir(lint::builtin::UNREACHABLE_CODE, id, span, &msg)
2350+
.span_note(
2351+
orig_span,
2352+
custom_note.unwrap_or("any code following this expression is unreachable")
2353+
)
2354+
.emit();
2355+
}
23212356
}
23222357
}
23232358

@@ -3825,7 +3860,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
38253860
//
38263861
// #41425 -- label the implicit `()` as being the
38273862
// "found type" here, rather than the "expected type".
3828-
if !self.diverges.get().always() {
3863+
if !self.diverges.get().is_always() {
38293864
// #50009 -- Do not point at the entire fn block span, point at the return type
38303865
// span, as it is the cause of the requirement, and
38313866
// `consider_hint_about_removing_semicolon` will point at the last expression

src/libstd/process.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,7 @@ impl Stdio {
935935
/// .expect("Failed to spawn child process");
936936
///
937937
/// {
938-
/// let mut stdin = child.stdin.as_mut().expect("Failed to open stdin");
938+
/// let stdin = child.stdin.as_mut().expect("Failed to open stdin");
939939
/// stdin.write_all("Hello, world!".as_bytes()).expect("Failed to write to stdin");
940940
/// }
941941
///

0 commit comments

Comments
 (0)