Skip to content

Commit b2a7713

Browse files
estebankcuviper
authored andcommitted
Emit specific message for time<0.3.35 inference failure
``` error[E0282]: type annotations needed for `Box<_>` --> ~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/time-0.3.34/src/format_description/parse/mod.rs:83:9 | 83 | let items = format_items | ^^^^^ ... 86 | Ok(items.into()) | ---- type must be known at this point | = note: this is an inference error on crate `time` caused by a change in Rust 1.80.0; update `time` to version `>=0.3.35` ``` Partially address rust-lang#127343. (cherry picked from commit b013a3d)
1 parent 4a765c0 commit b2a7713

File tree

7 files changed

+69
-2
lines changed

7 files changed

+69
-2
lines changed

compiler/rustc_infer/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,8 @@ infer_type_annotations_needed = {$source_kind ->
388388
}
389389
.label = type must be known at this point
390390
391+
infer_type_annotations_needed_error_time = this is an inference error on crate `time` caused by an API change in Rust 1.80.0; update `time` to version `>=0.3.35` by calling `cargo update`
392+
391393
infer_types_declared_different = these two types are declared with different lifetimes...
392394
393395
infer_warn_removing_apit_params = you could use a `use<...>` bound to explicitly capture `{$new_lifetime}`, but argument-position `impl Trait`s are not nameable

compiler/rustc_infer/src/errors/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ pub struct AnnotationRequired<'a> {
5454
#[note(infer_full_type_written)]
5555
pub was_written: Option<()>,
5656
pub path: PathBuf,
57+
#[note(infer_type_annotations_needed_error_time)]
58+
pub time_version: bool,
5759
}
5860

5961
// Copy of `AnnotationRequired` for E0283

compiler/rustc_infer/src/infer/need_type_info.rs

+44-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_errors::{codes::*, Diag, IntoDiagArg};
88
use rustc_hir as hir;
99
use rustc_hir::def::Res;
1010
use rustc_hir::def::{CtorOf, DefKind, Namespace};
11-
use rustc_hir::def_id::{DefId, LocalDefId};
11+
use rustc_hir::def_id::{DefId, LocalDefId, LOCAL_CRATE};
1212
use rustc_hir::intravisit::{self, Visitor};
1313
use rustc_hir::{Body, Closure, Expr, ExprKind, FnRetTy, HirId, LetStmt, LocalSource};
1414
use rustc_middle::bug;
@@ -21,7 +21,7 @@ use rustc_middle::ty::{
2121
TypeFoldable, TypeFolder, TypeSuperFoldable, TypeckResults,
2222
};
2323
use rustc_span::symbol::{sym, Ident};
24-
use rustc_span::{BytePos, Span, DUMMY_SP};
24+
use rustc_span::{BytePos, FileName, Span, DUMMY_SP};
2525
use std::borrow::Cow;
2626
use std::iter;
2727
use std::path::PathBuf;
@@ -409,6 +409,7 @@ impl<'tcx> InferCtxt<'tcx> {
409409
bad_label,
410410
was_written: None,
411411
path: Default::default(),
412+
time_version: false,
412413
}),
413414
TypeAnnotationNeeded::E0283 => self.dcx().create_err(AmbiguousImpl {
414415
span,
@@ -604,6 +605,10 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
604605
}
605606
}
606607
}
608+
609+
let time_version =
610+
self.detect_old_time_crate_version(failure_span, &kind, &mut infer_subdiags);
611+
607612
match error_code {
608613
TypeAnnotationNeeded::E0282 => self.dcx().create_err(AnnotationRequired {
609614
span,
@@ -615,6 +620,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
615620
bad_label: None,
616621
was_written: path.as_ref().map(|_| ()),
617622
path: path.unwrap_or_default(),
623+
time_version,
618624
}),
619625
TypeAnnotationNeeded::E0283 => self.dcx().create_err(AmbiguousImpl {
620626
span,
@@ -640,6 +646,42 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
640646
}),
641647
}
642648
}
649+
650+
/// Detect the inference regression on crate `time` <= 0.3.35 and emit a more targeted error.
651+
/// <https://github.com/rust-lang/rust/issues/127343>
652+
// FIXME: we should figure out a more generic version of doing this, ideally in cargo itself.
653+
fn detect_old_time_crate_version(
654+
&self,
655+
span: Option<Span>,
656+
kind: &InferSourceKind<'_>,
657+
// We will clear the non-actionable suggestion from the error to reduce noise.
658+
infer_subdiags: &mut Vec<SourceKindSubdiag<'_>>,
659+
) -> bool {
660+
// FIXME(#129461): We are time-boxing this code in the compiler. It'll start failing
661+
// compilation once we promote 1.89 to beta, which will happen in 9 months from now.
662+
#[cfg(not(version("1.89")))]
663+
const fn version_check() {}
664+
#[cfg(version("1.89"))]
665+
const fn version_check() {
666+
panic!("remove this check as presumably the ecosystem has moved from needing it");
667+
}
668+
const { version_check() };
669+
// Only relevant when building the `time` crate.
670+
if self.infcx.tcx.crate_name(LOCAL_CRATE) == sym::time
671+
&& let Some(span) = span
672+
&& let InferSourceKind::LetBinding { pattern_name, .. } = kind
673+
&& let Some(name) = pattern_name
674+
&& name.as_str() == "items"
675+
&& let FileName::Real(file) = self.infcx.tcx.sess.source_map().span_to_filename(span)
676+
{
677+
let path = file.local_path_if_available().to_string_lossy();
678+
if path.contains("format_description") && path.contains("parse") {
679+
infer_subdiags.clear();
680+
return true;
681+
}
682+
}
683+
false
684+
}
643685
}
644686

645687
#[derive(Debug)]

compiler/rustc_infer/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
2020
#![doc(rust_logo)]
2121
#![feature(box_patterns)]
22+
#![feature(cfg_version)]
2223
#![feature(control_flow_enum)]
2324
#![feature(extend_one)]
2425
#![feature(if_let_guard)]

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1883,6 +1883,7 @@ symbols! {
18831883
three_way_compare,
18841884
thumb2,
18851885
thumb_mode: "thumb-mode",
1886+
time,
18861887
tmm_reg,
18871888
to_owned_method,
18881889
to_string,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#![crate_name = "time"]
2+
3+
fn main() {
4+
let items = Box::new(vec![]); //~ ERROR E0282
5+
//~^ NOTE type must be known at this point
6+
//~| NOTE this is an inference error on crate `time` caused by an API change in Rust 1.80.0; update `time` to version `>=0.3.35`
7+
items.into();
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0282]: type annotations needed for `Box<Vec<_>>`
2+
--> $DIR/detect-old-time-version-format_description-parse.rs:4:9
3+
|
4+
LL | let items = Box::new(vec![]);
5+
| ^^^^^ ---------------- type must be known at this point
6+
|
7+
= note: this is an inference error on crate `time` caused by an API change in Rust 1.80.0; update `time` to version `>=0.3.35` by calling `cargo update`
8+
9+
error: aborting due to 1 previous error
10+
11+
For more information about this error, try `rustc --explain E0282`.

0 commit comments

Comments
 (0)