Skip to content

Commit a80e63f

Browse files
committed
Auto merge of #67917 - Dylan-DPC:rollup-id05y91, r=Dylan-DPC
Rollup of 6 pull requests Successful merges: - #67800 (Fix ICE involving calling `Instance.ty` during const evaluation) - #67873 (change remove to have a PartialEq bound) - #67897 (Use `as_deref()` to replace `as_ref().map(...)`) - #67906 (Silence `TooGeneric` error) - #67912 (macros: typo fix) - #67915 (Use Self instead of $type) Failed merges: r? @ghost
2 parents 33640f0 + 34716a3 commit a80e63f

File tree

27 files changed

+172
-51
lines changed

27 files changed

+172
-51
lines changed

src/liballoc/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#![feature(associated_type_bounds)]
1212
#![feature(binary_heap_into_iter_sorted)]
1313
#![feature(binary_heap_drain_sorted)]
14+
#![feature(vec_remove_item)]
1415

1516
use std::collections::hash_map::DefaultHasher;
1617
use std::hash::{Hash, Hasher};

src/liballoc/tests/vec.rs

+15
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,21 @@ fn test_extend_ref() {
131131
assert_eq!(v, [1, 2, 3, 4, 5, 6, 7]);
132132
}
133133

134+
#[test]
135+
fn test_remove_item() {
136+
let mut v = vec![1, 2, 3];
137+
v.remove_item(&1);
138+
139+
assert_eq!(v.len(), 2);
140+
assert_eq!(v, [2, 3]);
141+
142+
let mut w = vec![1, 2, 3];
143+
w.remove_item(&4);
144+
145+
assert_eq!(w.len(), 3);
146+
w.remove_item(&4);
147+
}
148+
134149
#[test]
135150
fn test_slice_from_mut() {
136151
let mut values = vec![1, 2, 3, 4, 5];

src/liballoc/vec.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1688,7 +1688,9 @@ impl<T: PartialEq> Vec<T> {
16881688
pub fn dedup(&mut self) {
16891689
self.dedup_by(|a, b| a == b)
16901690
}
1691+
}
16911692

1693+
impl<T> Vec<T> {
16921694
/// Removes the first instance of `item` from the vector if the item exists.
16931695
///
16941696
/// # Examples
@@ -1702,7 +1704,10 @@ impl<T: PartialEq> Vec<T> {
17021704
/// assert_eq!(vec, vec![2, 3, 1]);
17031705
/// ```
17041706
#[unstable(feature = "vec_remove_item", reason = "recently added", issue = "40062")]
1705-
pub fn remove_item(&mut self, item: &T) -> Option<T> {
1707+
pub fn remove_item<V>(&mut self, item: &V) -> Option<T>
1708+
where
1709+
T: PartialEq<V>,
1710+
{
17061711
let pos = self.iter().position(|x| *x == *item)?;
17071712
Some(self.remove(pos))
17081713
}

src/libcore/convert/num.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ macro_rules! impl_from {
4747
#[doc = $doc]
4848
impl From<$Small> for $Large {
4949
#[inline]
50-
fn from(small: $Small) -> $Large {
51-
small as $Large
50+
fn from(small: $Small) -> Self {
51+
small as Self
5252
}
5353
}
5454
};
@@ -177,7 +177,7 @@ macro_rules! try_from_unbounded {
177177
/// is outside of the range of the target type.
178178
#[inline]
179179
fn try_from(value: $source) -> Result<Self, Self::Error> {
180-
Ok(value as $target)
180+
Ok(value as Self)
181181
}
182182
}
183183
)*}
@@ -194,9 +194,9 @@ macro_rules! try_from_lower_bounded {
194194
/// number type. This returns an error if the source value
195195
/// is outside of the range of the target type.
196196
#[inline]
197-
fn try_from(u: $source) -> Result<$target, TryFromIntError> {
197+
fn try_from(u: $source) -> Result<Self, Self::Error> {
198198
if u >= 0 {
199-
Ok(u as $target)
199+
Ok(u as Self)
200200
} else {
201201
Err(TryFromIntError(()))
202202
}
@@ -216,11 +216,11 @@ macro_rules! try_from_upper_bounded {
216216
/// number type. This returns an error if the source value
217217
/// is outside of the range of the target type.
218218
#[inline]
219-
fn try_from(u: $source) -> Result<$target, TryFromIntError> {
220-
if u > (<$target>::max_value() as $source) {
219+
fn try_from(u: $source) -> Result<Self, Self::Error> {
220+
if u > (Self::max_value() as $source) {
221221
Err(TryFromIntError(()))
222222
} else {
223-
Ok(u as $target)
223+
Ok(u as Self)
224224
}
225225
}
226226
}
@@ -238,13 +238,13 @@ macro_rules! try_from_both_bounded {
238238
/// number type. This returns an error if the source value
239239
/// is outside of the range of the target type.
240240
#[inline]
241-
fn try_from(u: $source) -> Result<$target, TryFromIntError> {
242-
let min = <$target>::min_value() as $source;
243-
let max = <$target>::max_value() as $source;
241+
fn try_from(u: $source) -> Result<Self, Self::Error> {
242+
let min = Self::min_value() as $source;
243+
let max = Self::max_value() as $source;
244244
if u < min || u > max {
245245
Err(TryFromIntError(()))
246246
} else {
247-
Ok(u as $target)
247+
Ok(u as Self)
248248
}
249249
}
250250
}
@@ -385,10 +385,10 @@ macro_rules! nzint_impl_from {
385385
#[doc = $doc]
386386
impl From<$Small> for $Large {
387387
#[inline]
388-
fn from(small: $Small) -> $Large {
388+
fn from(small: $Small) -> Self {
389389
// SAFETY: input type guarantees the value is non-zero
390390
unsafe {
391-
<$Large>::new_unchecked(small.get().into())
391+
Self::new_unchecked(small.get().into())
392392
}
393393
}
394394
}

src/libcore/fmt/num.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ trait Int:
2424

2525
macro_rules! doit {
2626
($($t:ident)*) => ($(impl Int for $t {
27-
fn zero() -> $t { 0 }
28-
fn from_u8(u: u8) -> $t { u as $t }
27+
fn zero() -> Self { 0 }
28+
fn from_u8(u: u8) -> Self { u as Self }
2929
fn to_u8(&self) -> u8 { *self as u8 }
3030
fn to_u16(&self) -> u16 { *self as u16 }
3131
fn to_u32(&self) -> u32 { *self as u32 }

src/libcore/iter/traits/accum.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -44,28 +44,28 @@ macro_rules! integer_sum_product {
4444
(@impls $zero:expr, $one:expr, #[$attr:meta], $($a:ty)*) => ($(
4545
#[$attr]
4646
impl Sum for $a {
47-
fn sum<I: Iterator<Item=$a>>(iter: I) -> $a {
47+
fn sum<I: Iterator<Item=Self>>(iter: I) -> Self {
4848
iter.fold($zero, Add::add)
4949
}
5050
}
5151

5252
#[$attr]
5353
impl Product for $a {
54-
fn product<I: Iterator<Item=$a>>(iter: I) -> $a {
54+
fn product<I: Iterator<Item=Self>>(iter: I) -> Self {
5555
iter.fold($one, Mul::mul)
5656
}
5757
}
5858

5959
#[$attr]
6060
impl<'a> Sum<&'a $a> for $a {
61-
fn sum<I: Iterator<Item=&'a $a>>(iter: I) -> $a {
61+
fn sum<I: Iterator<Item=&'a Self>>(iter: I) -> Self {
6262
iter.fold($zero, Add::add)
6363
}
6464
}
6565

6666
#[$attr]
6767
impl<'a> Product<&'a $a> for $a {
68-
fn product<I: Iterator<Item=&'a $a>>(iter: I) -> $a {
68+
fn product<I: Iterator<Item=&'a Self>>(iter: I) -> Self {
6969
iter.fold($one, Mul::mul)
7070
}
7171
}
@@ -84,28 +84,28 @@ macro_rules! float_sum_product {
8484
($($a:ident)*) => ($(
8585
#[stable(feature = "iter_arith_traits", since = "1.12.0")]
8686
impl Sum for $a {
87-
fn sum<I: Iterator<Item=$a>>(iter: I) -> $a {
87+
fn sum<I: Iterator<Item=Self>>(iter: I) -> Self {
8888
iter.fold(0.0, Add::add)
8989
}
9090
}
9191

9292
#[stable(feature = "iter_arith_traits", since = "1.12.0")]
9393
impl Product for $a {
94-
fn product<I: Iterator<Item=$a>>(iter: I) -> $a {
94+
fn product<I: Iterator<Item=Self>>(iter: I) -> Self {
9595
iter.fold(1.0, Mul::mul)
9696
}
9797
}
9898

9999
#[stable(feature = "iter_arith_traits", since = "1.12.0")]
100100
impl<'a> Sum<&'a $a> for $a {
101-
fn sum<I: Iterator<Item=&'a $a>>(iter: I) -> $a {
101+
fn sum<I: Iterator<Item=&'a Self>>(iter: I) -> Self {
102102
iter.fold(0.0, Add::add)
103103
}
104104
}
105105

106106
#[stable(feature = "iter_arith_traits", since = "1.12.0")]
107107
impl<'a> Product<&'a $a> for $a {
108-
fn product<I: Iterator<Item=&'a $a>>(iter: I) -> $a {
108+
fn product<I: Iterator<Item=&'a Self>>(iter: I) -> Self {
109109
iter.fold(1.0, Mul::mul)
110110
}
111111
}

src/libcore/macros/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ macro_rules! unreachable {
551551

552552
/// Indicates unimplemented code by panicking with a message of "not implemented".
553553
///
554-
/// This allows the your code to type-check, which is useful if you are prototyping or
554+
/// This allows your code to type-check, which is useful if you are prototyping or
555555
/// implementing a trait that requires multiple methods which you don't plan of using all of.
556556
///
557557
/// The difference between `unimplemented!` and [`todo!`](macro.todo.html) is that while `todo!`

src/libcore/marker.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -505,15 +505,15 @@ macro_rules! impls {
505505

506506
#[stable(feature = "rust1", since = "1.0.0")]
507507
impl<T: ?Sized> Clone for $t<T> {
508-
fn clone(&self) -> $t<T> {
509-
$t
508+
fn clone(&self) -> Self {
509+
Self
510510
}
511511
}
512512

513513
#[stable(feature = "rust1", since = "1.0.0")]
514514
impl<T: ?Sized> Default for $t<T> {
515-
fn default() -> $t<T> {
516-
$t
515+
fn default() -> Self {
516+
Self
517517
}
518518
}
519519

src/libcore/num/bignum.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -455,8 +455,8 @@ macro_rules! define_bignum {
455455
}
456456

457457
impl crate::clone::Clone for $name {
458-
fn clone(&self) -> $name {
459-
$name { size: self.size, base: self.base }
458+
fn clone(&self) -> Self {
459+
Self { size: self.size, base: self.base }
460460
}
461461
}
462462

src/librustc/traits/error_reporting.rs

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::hir::Node;
1212
use crate::infer::error_reporting::TypeAnnotationNeeded as ErrorCode;
1313
use crate::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
1414
use crate::infer::{self, InferCtxt};
15+
use crate::mir::interpret::ErrorHandled;
1516
use crate::session::DiagnosticMessageId;
1617
use crate::ty::error::ExpectedFound;
1718
use crate::ty::fast_reject;
@@ -1086,6 +1087,11 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
10861087

10871088
// already reported in the query
10881089
ConstEvalFailure(err) => {
1090+
if let ErrorHandled::TooGeneric = err {
1091+
// Silence this error, as it can be produced during intermediate steps
1092+
// when a constant is not yet able to be evaluated (but will be later).
1093+
return;
1094+
}
10891095
self.tcx.sess.delay_span_bug(
10901096
span,
10911097
&format!("constant in type had an ignored error: {:?}", err),

src/librustc/ty/instance.rs

+26-1
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,35 @@ pub enum InstanceDef<'tcx> {
6262
}
6363

6464
impl<'tcx> Instance<'tcx> {
65-
pub fn ty(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
65+
/// Returns the `Ty` corresponding to this `Instance`,
66+
/// with generic substitutions applied and lifetimes erased.
67+
///
68+
/// This method can only be called when the 'substs' for this Instance
69+
/// are fully monomorphic (no `ty::Param`'s are present).
70+
/// This is usually the case (e.g. during codegen).
71+
/// However, during constant evaluation, we may want
72+
/// to try to resolve a `Instance` using generic parameters
73+
/// (e.g. when we are attempting to to do const-propagation).
74+
/// In this case, `Instance.ty_env` should be used to provide
75+
/// the `ParamEnv` for our generic context.
76+
pub fn monomorphic_ty(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
6677
let ty = tcx.type_of(self.def.def_id());
78+
// There shouldn't be any params - if there are, then
79+
// Instance.ty_env should have been used to provide the proper
80+
// ParamEnv
81+
if self.substs.has_param_types() {
82+
bug!("Instance.ty called for type {:?} with params in substs: {:?}", ty, self.substs);
83+
}
6784
tcx.subst_and_normalize_erasing_regions(self.substs, ty::ParamEnv::reveal_all(), &ty)
6885
}
86+
87+
/// Like `Instance.ty`, but allows a `ParamEnv` to be specified for use during
88+
/// normalization. This method is only really useful during constant evaluation,
89+
/// where we are dealing with potentially generic types.
90+
pub fn ty_env(&self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Ty<'tcx> {
91+
let ty = tcx.type_of(self.def.def_id());
92+
tcx.subst_and_normalize_erasing_regions(self.substs, param_env, &ty)
93+
}
6994
}
7095

7196
impl<'tcx> InstanceDef<'tcx> {

src/librustc/ty/layout.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2301,7 +2301,7 @@ impl<'tcx> ty::Instance<'tcx> {
23012301
// or should go through `FnAbi` instead, to avoid losing any
23022302
// adjustments `FnAbi::of_instance` might be performing.
23032303
fn fn_sig_for_fn_abi(&self, tcx: TyCtxt<'tcx>) -> ty::PolyFnSig<'tcx> {
2304-
let ty = self.ty(tcx);
2304+
let ty = self.monomorphic_ty(tcx);
23052305
match ty.kind {
23062306
ty::FnDef(..) |
23072307
// Shims currently have type FnPtr. Not sure this should remain.

src/librustc_codegen_llvm/callee.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub fn get_fn(cx: &CodegenCx<'ll, 'tcx>, instance: Instance<'tcx>) -> &'ll Value
3636
}
3737

3838
let sym = tcx.symbol_name(instance).name.as_str();
39-
debug!("get_fn({:?}: {:?}) => {}", instance, instance.ty(cx.tcx()), sym);
39+
debug!("get_fn({:?}: {:?}) => {}", instance, instance.monomorphic_ty(cx.tcx()), sym);
4040

4141
let fn_abi = FnAbi::of_instance(cx, instance, &[]);
4242

src/librustc_codegen_llvm/consts.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ impl CodegenCx<'ll, 'tcx> {
204204
def_id
205205
);
206206

207-
let ty = instance.ty(self.tcx);
207+
let ty = instance.monomorphic_ty(self.tcx);
208208
let sym = self.tcx.symbol_name(instance).name;
209209

210210
debug!("get_static: sym={} instance={:?}", sym, instance);
@@ -361,7 +361,7 @@ impl StaticMethods for CodegenCx<'ll, 'tcx> {
361361
};
362362

363363
let instance = Instance::mono(self.tcx, def_id);
364-
let ty = instance.ty(self.tcx);
364+
let ty = instance.monomorphic_ty(self.tcx);
365365
let llty = self.layout_of(ty).llvm_type(self);
366366
let g = if val_llty == llty {
367367
g

src/librustc_codegen_llvm/debuginfo/metadata.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2287,7 +2287,7 @@ pub fn create_global_var_metadata(cx: &CodegenCx<'ll, '_>, def_id: DefId, global
22872287
};
22882288

22892289
let is_local_to_unit = is_node_local_to_unit(cx, def_id);
2290-
let variable_type = Instance::mono(cx.tcx, def_id).ty(cx.tcx);
2290+
let variable_type = Instance::mono(cx.tcx, def_id).monomorphic_ty(cx.tcx);
22912291
let type_metadata = type_metadata(cx, variable_type, span);
22922292
let var_name = SmallCStr::new(&tcx.item_name(def_id).as_str());
22932293
let linkage_name = if no_mangle {

src/librustc_codegen_llvm/intrinsic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
8989
span: Span,
9090
) {
9191
let tcx = self.tcx;
92-
let callee_ty = instance.ty(tcx);
92+
let callee_ty = instance.monomorphic_ty(tcx);
9393

9494
let (def_id, substs) = match callee_ty.kind {
9595
ty::FnDef(def_id, substs) => (def_id, substs),

src/librustc_codegen_llvm/mono_item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl PreDefineMethods<'tcx> for CodegenCx<'ll, 'tcx> {
2222
symbol_name: &str,
2323
) {
2424
let instance = Instance::mono(self.tcx, def_id);
25-
let ty = instance.ty(self.tcx);
25+
let ty = instance.monomorphic_ty(self.tcx);
2626
let llty = self.layout_of(ty).llvm_type(self);
2727

2828
let g = self.define_global(symbol_name, llty).unwrap_or_else(|| {

src/librustc_mir/const_eval/eval_queries.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ pub fn const_eval_validated_provider<'tcx>(
221221
// We call `const_eval` for zero arg intrinsics, too, in order to cache their value.
222222
// Catch such calls and evaluate them instead of trying to load a constant's MIR.
223223
if let ty::InstanceDef::Intrinsic(def_id) = key.value.instance.def {
224-
let ty = key.value.instance.ty(tcx);
224+
let ty = key.value.instance.ty_env(tcx, key.param_env);
225225
let substs = match ty.kind {
226226
ty::FnDef(_, substs) => substs,
227227
_ => bug!("intrinsic with type {:?}", ty),

src/librustc_mir/interpret/terminator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
204204
// ABI check
205205
{
206206
let callee_abi = {
207-
let instance_ty = instance.ty(*self.tcx);
207+
let instance_ty = instance.ty_env(*self.tcx, self.param_env);
208208
match instance_ty.kind {
209209
ty::FnDef(..) => instance_ty.fn_sig(*self.tcx).abi(),
210210
ty::Closure(..) => Abi::RustCall,

src/librustc_mir/interpret/traits.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
140140
// to determine the type.
141141
let drop_instance = self.memory.get_fn(drop_fn)?.as_instance()?;
142142
trace!("Found drop fn: {:?}", drop_instance);
143-
let fn_sig = drop_instance.ty(*self.tcx).fn_sig(*self.tcx);
143+
let fn_sig = drop_instance.ty_env(*self.tcx, self.param_env).fn_sig(*self.tcx);
144144
let fn_sig = self.tcx.normalize_erasing_late_bound_regions(self.param_env, &fn_sig);
145145
// The drop function takes `*mut T` where `T` is the type being dropped, so get that.
146146
let args = fn_sig.inputs();

0 commit comments

Comments
 (0)