Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
f4c59b3
Use `drop_in_place` in `array::IntoIter::drop`
SimonSapin Oct 25, 2019
7550b61
Fix documentation for `Iterator::count()`.
ArturKovacs Nov 4, 2019
6ce3e1d
Fixed trailing whitespace.
ArturKovacs Nov 4, 2019
23be25c
Improve wording in the documentation of `Iterator::count()`.
ArturKovacs Nov 4, 2019
0282c27
rename cfg(rustdoc) into cfg(doc)
GuillaumeGomez Nov 6, 2019
b7c0e46
Add long error explanation for E0623
GuillaumeGomez Nov 7, 2019
ffc1c5a
Update ui tests
GuillaumeGomez Nov 7, 2019
413ab57
docs: Fix link to BufWriter::flush
bryanburgers Nov 8, 2019
1b8b2ee
add raw ptr variant of UnsafeCell::get
RalfJung Nov 9, 2019
8d56bcc
download .tar.xz if python3 is used
Oct 29, 2019
0019371
bootstrap: don't call support_xz in hot-path
Nov 11, 2019
f9f5a88
Add a callback that allows compiler consumers to override queries.
vakaras Nov 11, 2019
e1cf38a
Move injected_panic_runtime to CrateStore
Mark-Simulacrum Nov 11, 2019
2c6d609
Move allocator_kind to CrateStore
Mark-Simulacrum Nov 11, 2019
1aee3e4
Use a relative bindir for rustdoc to find rustc
cuviper Nov 11, 2019
db5fc10
[mir-opt] Turn on the `ConstProp` pass by default
wesleywiser Oct 28, 2019
e8f3a9f
add Result::map_or
tesuji Nov 11, 2019
eb99c73
Match constructor first in Constructor methods
Nadrieril Nov 12, 2019
357d53c
Introduce Constructor::NonExhaustive
Nadrieril Nov 12, 2019
e398d89
Move NonExhaustive checks to the relevant match branches
Nadrieril Nov 12, 2019
ec45882
Add test for issue-30904
JohnTitor Nov 12, 2019
bae9832
Add test for issue-40231
JohnTitor Nov 12, 2019
412f000
Add test for issue-52432
JohnTitor Nov 12, 2019
74d45af
Add test for issue-63279
JohnTitor Nov 12, 2019
bfa5e5f
Fallback to the unmodified path in bindir_relative
cuviper Nov 12, 2019
f696b21
Move self-profile infrastructure to data structures
Mark-Simulacrum Nov 11, 2019
8c29b74
Remove dead code for encoding/decoding lint IDs
Mark-Simulacrum Nov 12, 2019
2fd5454
Register queries with self profiler in rustc_interface
Mark-Simulacrum Nov 12, 2019
6a3a055
Update error_codes.rs
Dylan-DPC Nov 13, 2019
1de094a
Update error_codes.rs
Dylan-DPC Nov 13, 2019
fd868d4
tidy up!
Dylan-DPC Nov 13, 2019
aba385a
Trailing full stop
RalfJung Nov 13, 2019
19ebe2f
clarify why we can do the ptr cast
RalfJung Nov 13, 2019
5b5ae01
expand docs
RalfJung Nov 13, 2019
861698a
make things ugly
RalfJung Nov 13, 2019
c309266
Rollup merge of #65821 - SimonSapin:in-place, r=Amanieu
JohnTitor Nov 13, 2019
36b0aeb
Rollup merge of #65932 - guanqun:download-xz, r=alexcrichton
JohnTitor Nov 13, 2019
bfcf7f6
Rollup merge of #66074 - wesleywiser:test_run_const_prop, r=oli-obk
JohnTitor Nov 13, 2019
7c666a7
Rollup merge of #66094 - ArturKovacs:fix-count-doc, r=Dylan-DPC
JohnTitor Nov 13, 2019
1d89492
Rollup merge of #66166 - GuillaumeGomez:rename-rustdoc-to-doc, r=Quie…
JohnTitor Nov 13, 2019
0395fc7
Rollup merge of #66186 - GuillaumeGomez:long-err-explanation-E0623, r…
JohnTitor Nov 13, 2019
7c3562c
Rollup merge of #66227 - bryanburgers:bufwriter-docs-fix-flush-link, …
JohnTitor Nov 13, 2019
0310708
Rollup merge of #66248 - RalfJung:unsafe_cell_raw_get, r=SimonSapin
JohnTitor Nov 13, 2019
4a00361
Rollup merge of #66292 - lzutao:result-map_or, r=SimonSapin
JohnTitor Nov 13, 2019
891c054
Rollup merge of #66297 - vakaras:edit-queries, r=oli-obk
JohnTitor Nov 13, 2019
606c2ab
Rollup merge of #66317 - cuviper:bindir_relative, r=Mark-Simulacrum
JohnTitor Nov 13, 2019
92f3558
Rollup merge of #66330 - Nadrieril:nonexhaustive-constructor, r=varkor
JohnTitor Nov 13, 2019
9091d50
Rollup merge of #66331 - JohnTitor:add-tests, r=Centril
JohnTitor Nov 13, 2019
f037024
Rollup merge of #66334 - Mark-Simulacrum:sess-cstore, r=petrochenkov
JohnTitor Nov 13, 2019
23b4fba
Rollup merge of #66335 - Mark-Simulacrum:self-profile-to-data, r=mich…
JohnTitor Nov 13, 2019
96a9511
Rollup merge of #66337 - Mark-Simulacrum:no-decode-lint-id, r=Dylan-DPC
JohnTitor Nov 13, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Introduce Constructor::NonExhaustive
It counts as an extra constructor for types that are not allowed to be
matched exhaustively.
  • Loading branch information
Nadrieril committed Nov 12, 2019
commit 357d53c4ce6eed4d93fe1ed576613687d34d10b2
115 changes: 58 additions & 57 deletions src/librustc_mir/hair/pattern/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,13 +590,16 @@ enum Constructor<'tcx> {
FixedLenSlice(u64),
/// Slice patterns. Captures any array constructor of `length >= i + j`.
VarLenSlice(u64, u64),
/// Fake extra constructor for enums that aren't allowed to be matched exhaustively.
NonExhaustive,
}

// Ignore spans when comparing, they don't carry semantic information as they are only for lints.
impl<'tcx> std::cmp::PartialEq for Constructor<'tcx> {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Constructor::Single, Constructor::Single) => true,
(Constructor::NonExhaustive, Constructor::NonExhaustive) => true,
(Constructor::Variant(a), Constructor::Variant(b)) => a == b,
(Constructor::ConstantValue(a, _), Constructor::ConstantValue(b, _)) => a == b,
(
Expand Down Expand Up @@ -771,6 +774,8 @@ impl<'tcx> Constructor<'tcx> {
// ranges have been omitted.
remaining_ctors
}
// This constructor is never covered by anything else
NonExhaustive => vec![NonExhaustive],
}
}

Expand Down Expand Up @@ -842,7 +847,7 @@ impl<'tcx> Constructor<'tcx> {
}
_ => bug!("bad slice pattern {:?} {:?}", self, ty),
},
ConstantValue(..) | ConstantRange(..) => vec![],
ConstantValue(..) | ConstantRange(..) | NonExhaustive => vec![],
}
}

Expand All @@ -865,7 +870,7 @@ impl<'tcx> Constructor<'tcx> {
},
FixedLenSlice(length) => *length,
VarLenSlice(prefix, suffix) => prefix + suffix,
ConstantValue(..) | ConstantRange(..) => 0,
ConstantValue(..) | ConstantRange(..) | NonExhaustive => 0,
}
}

Expand Down Expand Up @@ -932,6 +937,7 @@ impl<'tcx> Constructor<'tcx> {
hi: ty::Const::from_bits(cx.tcx, hi, ty::ParamEnv::empty().and(ty)),
end,
}),
NonExhaustive => PatKind::Wild,
};

Pat { ty, span: DUMMY_SP, kind: Box::new(pat) }
Expand Down Expand Up @@ -1193,6 +1199,36 @@ fn all_constructors<'a, 'tcx>(
}
}
};

// FIXME: currently the only way I know of something can
// be a privately-empty enum is when the exhaustive_patterns
// feature flag is not present, so this is only
// needed for that case.
let is_privately_empty = ctors.is_empty() && !cx.is_uninhabited(pcx.ty);
let is_declared_nonexhaustive = cx.is_non_exhaustive_enum(pcx.ty) && !cx.is_local(pcx.ty);
let is_non_exhaustive = is_privately_empty
|| is_declared_nonexhaustive
|| (pcx.ty.is_ptr_sized_integral() && !cx.tcx.features().precise_pointer_size_matching);
if is_non_exhaustive {
// If our scrutinee is *privately* an empty enum, we must treat it as though it had an
// "unknown" constructor (in that case, all other patterns obviously can't be variants) to
// avoid exposing its emptyness. See the `match_privately_empty` test for details.
//
// If the enum is declared as `#[non_exhaustive]`, we treat it as if it had an additionnal
// "unknown" constructor. However there is no point in enumerating all possible variants,
// because the user can't actually match against them themselves. So we return only the
// fictitious constructor.
// E.g., in an example like:
// ```
// let err: io::ErrorKind = ...;
// match err {
// io::ErrorKind::NotFound => {},
// }
// ```
// we don't want to show every possible IO error, but instead have only `_` as the witness.
return vec![NonExhaustive];
}

ctors
}

Expand Down Expand Up @@ -1591,48 +1627,22 @@ pub fn is_useful<'p, 'a, 'tcx>(
let all_ctors = all_constructors(cx, pcx);
debug!("all_ctors = {:#?}", all_ctors);

let is_privately_empty = all_ctors.is_empty() && !cx.is_uninhabited(pcx.ty);
let is_declared_nonexhaustive = cx.is_non_exhaustive_enum(pcx.ty) && !cx.is_local(pcx.ty);

// `missing_ctors` is the set of constructors from the same type as the
// first column of `matrix` that are matched only by wildcard patterns
// from the first column.
//
// Therefore, if there is some pattern that is unmatched by `matrix`,
// it will still be unmatched if the first constructor is replaced by
// any of the constructors in `missing_ctors`
//
// However, if our scrutinee is *privately* an empty enum, we
// must treat it as though it had an "unknown" constructor (in
// that case, all other patterns obviously can't be variants)
// to avoid exposing its emptyness. See the `match_privately_empty`
// test for details.
//
// FIXME: currently the only way I know of something can
// be a privately-empty enum is when the exhaustive_patterns
// feature flag is not present, so this is only
// needed for that case.

// Missing constructors are those that are not matched by any
// non-wildcard patterns in the current column. To determine if
// the set is empty, we can check that `.peek().is_none()`, so
// we only fully construct them on-demand, because they're rarely used and can be big.
let missing_ctors = MissingConstructors::new(cx.tcx, cx.param_env, all_ctors, used_ctors);

debug!(
"missing_ctors.empty()={:#?} is_privately_empty={:#?} is_declared_nonexhaustive={:#?}",
missing_ctors.is_empty(),
is_privately_empty,
is_declared_nonexhaustive
);
// Missing constructors are those that are not matched by any non-wildcard patterns in the
// current column. We only fully construct them on-demand, because they're rarely used and
// can be big.
let missing_ctors = MissingConstructors::new(cx.tcx, cx.param_env, all_ctors, used_ctors);

// For privately empty and non-exhaustive enums, we work as if there were an "extra"
// `_` constructor for the type, so we can never match over all constructors.
let is_non_exhaustive = is_privately_empty
|| is_declared_nonexhaustive
|| (pcx.ty.is_ptr_sized_integral() && !cx.tcx.features().precise_pointer_size_matching);
debug!("missing_ctors.empty()={:#?}", missing_ctors.is_empty(),);

if missing_ctors.is_empty() && !is_non_exhaustive {
if missing_ctors.is_empty() {
let (all_ctors, _) = missing_ctors.into_inner();
split_grouped_constructors(cx.tcx, cx.param_env, pcx, all_ctors, matrix, DUMMY_SP, None)
.into_iter()
Expand Down Expand Up @@ -1661,26 +1671,9 @@ pub fn is_useful<'p, 'a, 'tcx>(
//
// we can report 3 witnesses: `S`, `E`, and `W`.
//
// However, there are 2 cases where we don't want
// However, there is a case where we don't want
// to do this and instead report a single `_` witness:
//
// 1) If the user is matching against a non-exhaustive
// enum, there is no point in enumerating all possible
// variants, because the user can't actually match
// against them themselves, e.g., in an example like:
// ```
// let err: io::ErrorKind = ...;
// match err {
// io::ErrorKind::NotFound => {},
// }
// ```
// we don't want to show every possible IO error,
// but instead have `_` as the witness (this is
// actually *required* if the user specified *all*
// IO errors, but is probably what we want in every
// case).
//
// 2) If the user didn't actually specify a constructor
// if the user didn't actually specify a constructor
// in this arm, e.g., in
// ```
// let x: (Direction, Direction, bool) = ...;
Expand All @@ -1690,7 +1683,7 @@ pub fn is_useful<'p, 'a, 'tcx>(
// `(<direction-1>, <direction-2>, true)` - we are
// satisfied with `(_, _, true)`. In this case,
// `used_ctors` is empty.
if is_non_exhaustive || missing_ctors.all_ctors_are_missing() {
if missing_ctors.all_ctors_are_missing() {
// All constructors are unused. Add a wild pattern
// rather than each individual constructor.
usefulness.apply_wildcard(pcx.ty)
Expand Down Expand Up @@ -2217,13 +2210,21 @@ fn patterns_for_variant<'p, 'a: 'p, 'tcx>(
/// fields filled with wild patterns.
fn specialize_one_pattern<'p, 'a: 'p, 'q: 'p, 'tcx>(
cx: &mut MatchCheckCtxt<'a, 'tcx>,
pat: &'q Pat<'tcx>,
mut pat: &'q Pat<'tcx>,
constructor: &Constructor<'tcx>,
ctor_wild_subpatterns: &[&'p Pat<'tcx>],
) -> Option<PatStack<'p, 'tcx>> {
while let PatKind::AscribeUserType { ref subpattern, .. } = *pat.kind {
pat = subpattern;
}

if let NonExhaustive = constructor {
// Only a wildcard pattern can match the special extra constructor
return if pat.is_wildcard() { Some(PatStack::default()) } else { None };
}

let result = match *pat.kind {
PatKind::AscribeUserType { ref subpattern, .. } => PatStack::from_pattern(subpattern)
.specialize_constructor(cx, constructor, ctor_wild_subpatterns),
PatKind::AscribeUserType { .. } => bug!(), // Handled above

PatKind::Binding { .. } | PatKind::Wild => {
Some(PatStack::from_slice(ctor_wild_subpatterns))
Expand Down