Skip to content

Commit 42acd90

Browse files
committed
Auto merge of rust-lang#72346 - Dylan-DPC:rollup-vp418xs, r=Dylan-DPC
Rollup of 5 pull requests Successful merges: - rust-lang#71886 (Stabilize saturating_abs and saturating_neg) - rust-lang#72066 (correctly handle uninferred consts) - rust-lang#72068 (Ignore arguments when looking for `IndexMut` for subsequent `mut` obligation) - rust-lang#72338 (Fix ICE in -Zsave-analysis) - rust-lang#72344 (Assert doc wording) Failed merges: r? @ghost
2 parents 914adf0 + 745ca2a commit 42acd90

File tree

13 files changed

+154
-16
lines changed

13 files changed

+154
-16
lines changed

src/libcore/macros/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1243,7 +1243,7 @@ pub(crate) mod builtin {
12431243
/// be disabled. See [`debug_assert!`] for assertions that are not enabled in
12441244
/// release builds by default.
12451245
///
1246-
/// Unsafe code relies on `assert!` to enforce run-time invariants that, if
1246+
/// Unsafe code may rely on `assert!` to enforce run-time invariants that, if
12471247
/// violated could lead to unsafety.
12481248
///
12491249
/// Other use-cases of `assert!` include testing and enforcing run-time

src/libcore/num/mod.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -1165,8 +1165,7 @@ instead of overflowing.
11651165
Basic usage:
11661166
11671167
```
1168-
", $Feature, "#![feature(saturating_neg)]
1169-
assert_eq!(100", stringify!($SelfT), ".saturating_neg(), -100);
1168+
", $Feature, "assert_eq!(100", stringify!($SelfT), ".saturating_neg(), -100);
11701169
assert_eq!((-100", stringify!($SelfT), ").saturating_neg(), 100);
11711170
assert_eq!(", stringify!($SelfT), "::MIN.saturating_neg(), ", stringify!($SelfT),
11721171
"::MAX);
@@ -1175,7 +1174,7 @@ assert_eq!(", stringify!($SelfT), "::MAX.saturating_neg(), ", stringify!($SelfT)
11751174
$EndFeature, "
11761175
```"),
11771176

1178-
#[unstable(feature = "saturating_neg", issue = "59983")]
1177+
#[stable(feature = "saturating_neg", since = "1.45.0")]
11791178
#[rustc_const_unstable(feature = "const_saturating_int_methods", issue = "53718")]
11801179
#[inline]
11811180
pub const fn saturating_neg(self) -> Self {
@@ -1192,8 +1191,7 @@ MIN` instead of overflowing.
11921191
Basic usage:
11931192
11941193
```
1195-
", $Feature, "#![feature(saturating_neg)]
1196-
assert_eq!(100", stringify!($SelfT), ".saturating_abs(), 100);
1194+
", $Feature, "assert_eq!(100", stringify!($SelfT), ".saturating_abs(), 100);
11971195
assert_eq!((-100", stringify!($SelfT), ").saturating_abs(), 100);
11981196
assert_eq!(", stringify!($SelfT), "::MIN.saturating_abs(), ", stringify!($SelfT),
11991197
"::MAX);
@@ -1202,7 +1200,7 @@ assert_eq!((", stringify!($SelfT), "::MIN + 1).saturating_abs(), ", stringify!($
12021200
$EndFeature, "
12031201
```"),
12041202

1205-
#[unstable(feature = "saturating_neg", issue = "59983")]
1203+
#[stable(feature = "saturating_neg", since = "1.45.0")]
12061204
#[rustc_const_unstable(feature = "const_saturating_int_methods", issue = "53718")]
12071205
#[inline]
12081206
pub const fn saturating_abs(self) -> Self {

src/libcore/tests/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#![feature(pattern)]
1818
#![feature(range_is_empty)]
1919
#![feature(raw)]
20-
#![feature(saturating_neg)]
2120
#![feature(sort_internals)]
2221
#![feature(slice_partition_at_index)]
2322
#![feature(specialization)]

src/librustc_infer/infer/error_reporting/need_type_info.rs

+42-1
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,19 @@ fn closure_args(fn_sig: &ty::PolyFnSig<'_>) -> String {
172172
}
173173

174174
pub enum TypeAnnotationNeeded {
175+
/// ```compile_fail,E0282
176+
/// let x = "hello".chars().rev().collect();
177+
/// ```
175178
E0282,
179+
/// An implementation cannot be chosen unambiguously because of lack of information.
180+
/// ```compile_fail,E0283
181+
/// let _ = Default::default();
182+
/// ```
176183
E0283,
184+
/// ```compile_fail,E0284
185+
/// let mut d: u64 = 2;
186+
/// d = d % 1u32.into();
187+
/// ```
177188
E0284,
178189
}
179190

@@ -261,7 +272,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
261272
printer.name_resolver = Some(Box::new(&getter));
262273
let _ = if let ty::FnDef(..) = ty.kind {
263274
// We don't want the regular output for `fn`s because it includes its path in
264-
// invalid pseduo-syntax, we want the `fn`-pointer output instead.
275+
// invalid pseudo-syntax, we want the `fn`-pointer output instead.
265276
ty.fn_sig(self.tcx).print(printer)
266277
} else {
267278
ty.print(printer)
@@ -518,6 +529,36 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
518529
err
519530
}
520531

532+
// FIXME(const_generics): We should either try and merge this with `need_type_info_err`
533+
// or improve the errors created here.
534+
//
535+
// Unlike for type inference variables, we don't yet store the origin of const inference variables.
536+
// This is needed for to get a more relevant error span.
537+
pub fn need_type_info_err_const(
538+
&self,
539+
body_id: Option<hir::BodyId>,
540+
span: Span,
541+
ct: &'tcx ty::Const<'tcx>,
542+
error_code: TypeAnnotationNeeded,
543+
) -> DiagnosticBuilder<'tcx> {
544+
let mut local_visitor = FindHirNodeVisitor::new(&self, ct.into(), span);
545+
if let Some(body_id) = body_id {
546+
let expr = self.tcx.hir().expect_expr(body_id.hir_id);
547+
local_visitor.visit_expr(expr);
548+
}
549+
550+
let error_code = error_code.into();
551+
let mut err = self.tcx.sess.struct_span_err_with_code(
552+
local_visitor.target_span,
553+
&format!("type annotations needed"),
554+
error_code,
555+
);
556+
557+
err.note("unable to infer the value of a const parameter");
558+
559+
err
560+
}
561+
521562
/// If the `FnSig` for the method call can be found and type arguments are identified as
522563
/// needed, suggest annotating the call, otherwise point out the resulting type of the call.
523564
fn annotate_method_call(

src/librustc_save_analysis/lib.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,11 @@ impl<'l, 'tcx> SaveContext<'l, 'tcx> {
620620
}
621621

622622
pub fn get_path_res(&self, id: NodeId) -> Res {
623-
let hir_id = self.tcx.hir().node_id_to_hir_id(id);
623+
// FIXME(#71104)
624+
let hir_id = match self.tcx.hir().opt_node_id_to_hir_id(id) {
625+
Some(id) => id,
626+
None => return Res::Err,
627+
};
624628
match self.tcx.hir().get(hir_id) {
625629
Node::TraitRef(tr) => tr.path.res,
626630

src/librustc_typeck/check/method/confirm.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,9 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
468468

469469
match expr.kind {
470470
hir::ExprKind::Index(ref base_expr, ref index_expr) => {
471-
let index_expr_ty = self.node_ty(index_expr.hir_id);
471+
// We need to get the final type in case dereferences were needed for the trait
472+
// to apply (#72002).
473+
let index_expr_ty = self.tables.borrow().expr_ty_adjusted(index_expr);
472474
self.convert_place_op_to_mutable(
473475
PlaceOp::Index,
474476
expr,

src/librustc_typeck/check/writeback.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -647,13 +647,26 @@ impl<'cx, 'tcx> Resolver<'cx, 'tcx> {
647647
Resolver { tcx: fcx.tcx, infcx: fcx, span, body, replaced_with_error: false }
648648
}
649649

650-
fn report_error(&self, t: Ty<'tcx>) {
650+
fn report_type_error(&self, t: Ty<'tcx>) {
651651
if !self.tcx.sess.has_errors() {
652652
self.infcx
653653
.need_type_info_err(Some(self.body.id()), self.span.to_span(self.tcx), t, E0282)
654654
.emit();
655655
}
656656
}
657+
658+
fn report_const_error(&self, c: &'tcx ty::Const<'tcx>) {
659+
if !self.tcx.sess.has_errors() {
660+
self.infcx
661+
.need_type_info_err_const(
662+
Some(self.body.id()),
663+
self.span.to_span(self.tcx),
664+
c,
665+
E0282,
666+
)
667+
.emit();
668+
}
669+
}
657670
}
658671

659672
impl<'cx, 'tcx> TypeFolder<'tcx> for Resolver<'cx, 'tcx> {
@@ -666,7 +679,7 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Resolver<'cx, 'tcx> {
666679
Ok(t) => self.infcx.tcx.erase_regions(&t),
667680
Err(_) => {
668681
debug!("Resolver::fold_ty: input type `{:?}` not fully resolvable", t);
669-
self.report_error(t);
682+
self.report_type_error(t);
670683
self.replaced_with_error = true;
671684
self.tcx().types.err
672685
}
@@ -683,8 +696,7 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Resolver<'cx, 'tcx> {
683696
Ok(ct) => self.infcx.tcx.erase_regions(&ct),
684697
Err(_) => {
685698
debug!("Resolver::fold_const: input const `{:?}` not fully resolvable", ct);
686-
// FIXME: we'd like to use `self.report_error`, but it doesn't yet
687-
// accept a &'tcx ty::Const.
699+
self.report_const_error(ct);
688700
self.replaced_with_error = true;
689701
self.tcx().mk_const(ty::Const { val: ty::ConstKind::Error, ty: ct.ty })
690702
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#![feature(const_generics)]
2+
//~^ WARN the feature `const_generics` is incomplete
3+
4+
// taken from https://github.com/rust-lang/rust/issues/70507#issuecomment-615268893
5+
struct Foo;
6+
impl Foo {
7+
fn foo<const N: usize>(self) {}
8+
}
9+
fn main() {
10+
Foo.foo();
11+
//~^ ERROR type annotations needed
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
warning: the feature `const_generics` is incomplete and may not be safe to use and/or cause compiler crashes
2+
--> $DIR/uninferred-consts.rs:1:12
3+
|
4+
LL | #![feature(const_generics)]
5+
| ^^^^^^^^^^^^^^
6+
|
7+
= note: `#[warn(incomplete_features)]` on by default
8+
= note: see issue #44580 <https://github.com/rust-lang/rust/issues/44580> for more information
9+
10+
error[E0282]: type annotations needed
11+
--> $DIR/uninferred-consts.rs:10:5
12+
|
13+
LL | Foo.foo();
14+
| ^^^^^^^^^
15+
|
16+
= note: unable to infer the value of a const parameter
17+
18+
error: aborting due to previous error; 1 warning emitted
19+
20+
For more information about this error, try `rustc --explain E0282`.

src/test/ui/consts/const-int-arithmetic.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// run-pass
22

3-
#![feature(saturating_neg)]
43
#![feature(const_checked_int_methods)]
54
#![feature(const_euclidean_int_methods)]
65
#![feature(const_overflowing_int_methods)]

src/test/ui/issues/issue-72002.rs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// check-pass
2+
struct Indexable;
3+
4+
impl Indexable {
5+
fn boo(&mut self) {}
6+
}
7+
8+
impl std::ops::Index<&str> for Indexable {
9+
type Output = Indexable;
10+
11+
fn index(&self, field: &str) -> &Indexable {
12+
self
13+
}
14+
}
15+
16+
impl std::ops::IndexMut<&str> for Indexable {
17+
fn index_mut(&mut self, field: &str) -> &mut Indexable {
18+
self
19+
}
20+
}
21+
22+
fn main() {
23+
let mut v = Indexable;
24+
let field = "hello".to_string();
25+
26+
v[field.as_str()].boo();
27+
28+
v[&field].boo(); // < This should work
29+
}
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// compile-flags: -Z save-analysis
2+
3+
fn main() {
4+
let _: Box<(dyn ?Sized)>;
5+
//~^ ERROR `?Trait` is not permitted in trait object types
6+
//~| ERROR at least one trait is required for an object type
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: `?Trait` is not permitted in trait object types
2+
--> $DIR/issue-72267.rs:4:21
3+
|
4+
LL | let _: Box<(dyn ?Sized)>;
5+
| ^^^^^^
6+
7+
error[E0224]: at least one trait is required for an object type
8+
--> $DIR/issue-72267.rs:4:17
9+
|
10+
LL | let _: Box<(dyn ?Sized)>;
11+
| ^^^^^^^^^^
12+
13+
error: aborting due to 2 previous errors
14+
15+
For more information about this error, try `rustc --explain E0224`.

0 commit comments

Comments
 (0)