Skip to content

Commit 033662d

Browse files
committed
Auto merge of #67220 - Centril:rollup-n3u9wd5, r=Centril
Rollup of 6 pull requests Successful merges: - #66881 (Optimize Ord trait implementation for bool) - #67015 (Fix constant propagation for scalar pairs) - #67074 (Add options to --extern flag.) - #67164 (Ensure that panicking in constants eventually errors) - #67174 (Remove `checked_add` in `Layout::repeat`) - #67205 (Make `publish_toolstate.sh` executable) Failed merges: r? @ghost
2 parents ddca1e0 + f6ceef5 commit 033662d

40 files changed

+684
-211
lines changed

src/ci/publish_toolstate.sh

100644100755
File mode changed.

src/libcore/alloc.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,11 @@ impl Layout {
239239
#[unstable(feature = "alloc_layout_extra", issue = "55724")]
240240
#[inline]
241241
pub fn repeat(&self, n: usize) -> Result<(Self, usize), LayoutErr> {
242-
let padded_size = self.size().checked_add(self.padding_needed_for(self.align()))
243-
.ok_or(LayoutErr { private: () })?;
242+
// This cannot overflow. Quoting from the invariant of Layout:
243+
// > `size`, when rounded up to the nearest multiple of `align`,
244+
// > must not overflow (i.e., the rounded value must be less than
245+
// > `usize::MAX`)
246+
let padded_size = self.size() + self.padding_needed_for(self.align());
244247
let alloc_size = padded_size.checked_mul(n)
245248
.ok_or(LayoutErr { private: () })?;
246249

src/libcore/cmp.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,7 @@ pub fn max_by_key<T, F: FnMut(&T) -> K, K: Ord>(v1: T, v2: T, mut f: F) -> T {
10051005

10061006
// Implementation of PartialEq, Eq, PartialOrd and Ord for primitive types
10071007
mod impls {
1008+
use crate::hint::unreachable_unchecked;
10081009
use crate::cmp::Ordering::{self, Less, Greater, Equal};
10091010

10101011
macro_rules! partial_eq_impl {
@@ -1125,7 +1126,16 @@ mod impls {
11251126
impl Ord for bool {
11261127
#[inline]
11271128
fn cmp(&self, other: &bool) -> Ordering {
1128-
(*self as u8).cmp(&(*other as u8))
1129+
// Casting to i8's and converting the difference to an Ordering generates
1130+
// more optimal assembly.
1131+
// See <https://github.com/rust-lang/rust/issues/66780> for more info.
1132+
match (*self as i8) - (*other as i8) {
1133+
-1 => Less,
1134+
0 => Equal,
1135+
1 => Greater,
1136+
// SAFETY: bool as i8 returns 0 or 1, so the difference can't be anything else
1137+
_ => unsafe { unreachable_unchecked() },
1138+
}
11291139
}
11301140
}
11311141

src/libcore/tests/cmp.rs

+8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ fn test_int_totalord() {
99
assert_eq!(12.cmp(&-5), Greater);
1010
}
1111

12+
#[test]
13+
fn test_bool_totalord() {
14+
assert_eq!(true.cmp(&false), Greater);
15+
assert_eq!(false.cmp(&true), Less);
16+
assert_eq!(true.cmp(&true), Equal);
17+
assert_eq!(false.cmp(&false), Equal);
18+
}
19+
1220
#[test]
1321
fn test_mut_int_totalord() {
1422
assert_eq!((&mut 5).cmp(&&mut 10), Less);

src/librustc_interface/tests.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc::middle::cstore;
77
use rustc::session::config::{build_configuration, build_session_options, to_crate_config};
88
use rustc::session::config::{LtoCli, LinkerPluginLto, SwitchWithOptPath, ExternEntry};
99
use rustc::session::config::{Externs, OutputType, OutputTypes, SymbolManglingVersion};
10-
use rustc::session::config::{rustc_optgroups, Options, ErrorOutputType, Passes};
10+
use rustc::session::config::{rustc_optgroups, Options, ErrorOutputType, Passes, ExternLocation};
1111
use rustc::session::{build_session, Session};
1212
use rustc::session::search_paths::SearchPath;
1313
use std::collections::{BTreeMap, BTreeSet};
@@ -38,14 +38,15 @@ fn mk_session(matches: getopts::Matches) -> (Session, CfgSpecs) {
3838
fn new_public_extern_entry<S, I>(locations: I) -> ExternEntry
3939
where
4040
S: Into<String>,
41-
I: IntoIterator<Item = Option<S>>,
41+
I: IntoIterator<Item = S>,
4242
{
43-
let locations: BTreeSet<_> = locations.into_iter().map(|o| o.map(|s| s.into()))
43+
let locations: BTreeSet<_> = locations.into_iter().map(|s| s.into())
4444
.collect();
4545

4646
ExternEntry {
47-
locations,
48-
is_private_dep: false
47+
location: ExternLocation::ExactPaths(locations),
48+
is_private_dep: false,
49+
add_prelude: true,
4950
}
5051
}
5152

@@ -160,33 +161,33 @@ fn test_externs_tracking_hash_different_construction_order() {
160161
v1.externs = Externs::new(mk_map(vec![
161162
(
162163
String::from("a"),
163-
new_public_extern_entry(vec![Some("b"), Some("c")])
164+
new_public_extern_entry(vec!["b", "c"])
164165
),
165166
(
166167
String::from("d"),
167-
new_public_extern_entry(vec![Some("e"), Some("f")])
168+
new_public_extern_entry(vec!["e", "f"])
168169
),
169170
]));
170171

171172
v2.externs = Externs::new(mk_map(vec![
172173
(
173174
String::from("d"),
174-
new_public_extern_entry(vec![Some("e"), Some("f")])
175+
new_public_extern_entry(vec!["e", "f"])
175176
),
176177
(
177178
String::from("a"),
178-
new_public_extern_entry(vec![Some("b"), Some("c")])
179+
new_public_extern_entry(vec!["b", "c"])
179180
),
180181
]));
181182

182183
v3.externs = Externs::new(mk_map(vec![
183184
(
184185
String::from("a"),
185-
new_public_extern_entry(vec![Some("b"), Some("c")])
186+
new_public_extern_entry(vec!["b", "c"])
186187
),
187188
(
188189
String::from("d"),
189-
new_public_extern_entry(vec![Some("f"), Some("e")])
190+
new_public_extern_entry(vec!["f", "e"])
190191
),
191192
]));
192193

src/librustc_metadata/creader.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,14 @@ impl<'a> CrateLoader<'a> {
218218
let source = self.cstore.get_crate_data(cnum).source();
219219
if let Some(entry) = self.sess.opts.externs.get(&name.as_str()) {
220220
// Only use `--extern crate_name=path` here, not `--extern crate_name`.
221-
let found = entry.locations.iter().filter_map(|l| l.as_ref()).any(|l| {
222-
let l = fs::canonicalize(l).ok();
223-
source.dylib.as_ref().map(|p| &p.0) == l.as_ref() ||
224-
source.rlib.as_ref().map(|p| &p.0) == l.as_ref()
225-
});
226-
if found {
227-
ret = Some(cnum);
221+
if let Some(mut files) = entry.files() {
222+
if files.any(|l| {
223+
let l = fs::canonicalize(l).ok();
224+
source.dylib.as_ref().map(|p| &p.0) == l.as_ref() ||
225+
source.rlib.as_ref().map(|p| &p.0) == l.as_ref()
226+
}) {
227+
ret = Some(cnum);
228+
}
228229
}
229230
return
230231
}

src/librustc_metadata/locator.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,9 @@ impl<'a> CrateLocator<'a> {
328328
crate_name,
329329
exact_paths: if hash.is_none() {
330330
sess.opts.externs.get(&crate_name.as_str()).into_iter()
331-
.flat_map(|entry| entry.locations.iter())
332-
.filter_map(|location| location.clone().map(PathBuf::from)).collect()
331+
.filter_map(|entry| entry.files())
332+
.flatten()
333+
.map(|location| PathBuf::from(location)).collect()
333334
} else {
334335
// SVH being specified means this is a transitive dependency,
335336
// so `--extern` options do not apply.

src/librustc_mir/build/expr/into.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
6565
_ => false,
6666
};
6767

68-
unpack!(block = this.as_local_rvalue(block, source));
68+
// (#66975) Source could be a const of type `!`, so has to
69+
// exist in the generated MIR.
70+
unpack!(block = this.as_temp(
71+
block,
72+
this.local_scope(),
73+
source,
74+
Mutability::Mut,
75+
));
6976

7077
// This is an optimization. If the expression was a call then we already have an
7178
// unreachable block. Don't bother to terminate it and create a new one.

src/librustc_mir/transform/const_prop.rs

+37-11
Original file line numberDiff line numberDiff line change
@@ -636,19 +636,45 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
636636
ScalarMaybeUndef::Scalar(one),
637637
ScalarMaybeUndef::Scalar(two)
638638
) => {
639+
// Found a value represented as a pair. For now only do cont-prop if type of
640+
// Rvalue is also a pair with two scalars. The more general case is more
641+
// complicated to implement so we'll do it later.
639642
let ty = &value.layout.ty.kind;
643+
// Only do it for tuples
640644
if let ty::Tuple(substs) = ty {
641-
*rval = Rvalue::Aggregate(
642-
Box::new(AggregateKind::Tuple),
643-
vec![
644-
self.operand_from_scalar(
645-
one, substs[0].expect_ty(), source_info.span
646-
),
647-
self.operand_from_scalar(
648-
two, substs[1].expect_ty(), source_info.span
649-
),
650-
],
651-
);
645+
// Only do it if tuple is also a pair with two scalars
646+
if substs.len() == 2 {
647+
let opt_ty1_ty2 = self.use_ecx(source_info, |this| {
648+
let ty1 = substs[0].expect_ty();
649+
let ty2 = substs[1].expect_ty();
650+
let ty_is_scalar = |ty| {
651+
this.ecx
652+
.layout_of(ty)
653+
.ok()
654+
.map(|ty| ty.details.abi.is_scalar())
655+
== Some(true)
656+
};
657+
if ty_is_scalar(ty1) && ty_is_scalar(ty2) {
658+
Ok(Some((ty1, ty2)))
659+
} else {
660+
Ok(None)
661+
}
662+
});
663+
664+
if let Some(Some((ty1, ty2))) = opt_ty1_ty2 {
665+
*rval = Rvalue::Aggregate(
666+
Box::new(AggregateKind::Tuple),
667+
vec![
668+
self.operand_from_scalar(
669+
one, ty1, source_info.span
670+
),
671+
self.operand_from_scalar(
672+
two, ty2, source_info.span
673+
),
674+
],
675+
);
676+
}
677+
}
652678
}
653679
},
654680
_ => { }

src/librustc_resolve/lib.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1136,8 +1136,10 @@ impl<'a> Resolver<'a> {
11361136
definitions.create_root_def(crate_name, session.local_crate_disambiguator());
11371137

11381138
let mut extern_prelude: FxHashMap<Ident, ExternPreludeEntry<'_>> =
1139-
session.opts.externs.iter().map(|kv| (Ident::from_str(kv.0), Default::default()))
1140-
.collect();
1139+
session.opts.externs.iter()
1140+
.filter(|(_, entry)| entry.add_prelude)
1141+
.map(|(name, _)| (Ident::from_str(name), Default::default()))
1142+
.collect();
11411143

11421144
if !attr::contains_name(&krate.attrs, sym::no_core) {
11431145
extern_prelude.insert(Ident::with_dummy_span(sym::core), Default::default());

0 commit comments

Comments
 (0)