Skip to content

Commit b013a3d

Browse files
committed
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 #127343.
1 parent 100fde5 commit b013a3d

File tree

7 files changed

+69
-2
lines changed

7 files changed

+69
-2
lines changed

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1896,6 +1896,7 @@ symbols! {
18961896
three_way_compare,
18971897
thumb2,
18981898
thumb_mode: "thumb-mode",
1899+
time,
18991900
tmm_reg,
19001901
to_owned_method,
19011902
to_string,

compiler/rustc_trait_selection/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,8 @@ trait_selection_type_annotations_needed = {$source_kind ->
446446
}
447447
.label = type must be known at this point
448448
449+
trait_selection_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`
450+
449451
trait_selection_types_declared_different = these two types are declared with different lifetimes...
450452
451453
trait_selection_unable_to_construct_constant_value = unable to construct a constant value for the unevaluated constant {$unevaluated}

compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs

+44-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_errors::codes::*;
66
use rustc_errors::{Diag, IntoDiagArg};
77
use rustc_hir as hir;
88
use rustc_hir::def::{CtorOf, DefKind, Namespace, Res};
9-
use rustc_hir::def_id::{DefId, LocalDefId};
9+
use rustc_hir::def_id::{DefId, LocalDefId, LOCAL_CRATE};
1010
use rustc_hir::intravisit::{self, Visitor};
1111
use rustc_hir::{Body, Closure, Expr, ExprKind, FnRetTy, HirId, LetStmt, LocalSource};
1212
use rustc_middle::bug;
@@ -18,7 +18,7 @@ use rustc_middle::ty::{
1818
TypeFoldable, TypeFolder, TypeSuperFoldable, TypeckResults,
1919
};
2020
use rustc_span::symbol::{sym, Ident};
21-
use rustc_span::{BytePos, Span, DUMMY_SP};
21+
use rustc_span::{BytePos, FileName, Span, DUMMY_SP};
2222

2323
use crate::error_reporting::TypeErrCtxt;
2424
use crate::errors::{
@@ -384,6 +384,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
384384
bad_label,
385385
was_written: false,
386386
path: Default::default(),
387+
time_version: false,
387388
}),
388389
TypeAnnotationNeeded::E0283 => self.dcx().create_err(AmbiguousImpl {
389390
span,
@@ -577,6 +578,10 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
577578
}
578579
}
579580
}
581+
582+
let time_version =
583+
self.detect_old_time_crate_version(failure_span, &kind, &mut infer_subdiags);
584+
580585
match error_code {
581586
TypeAnnotationNeeded::E0282 => self.dcx().create_err(AnnotationRequired {
582587
span,
@@ -588,6 +593,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
588593
bad_label: None,
589594
was_written: path.is_some(),
590595
path: path.unwrap_or_default(),
596+
time_version,
591597
}),
592598
TypeAnnotationNeeded::E0283 => self.dcx().create_err(AmbiguousImpl {
593599
span,
@@ -613,6 +619,42 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
613619
}),
614620
}
615621
}
622+
623+
/// Detect the inference regression on crate `time` <= 0.3.35 and emit a more targeted error.
624+
/// <https://github.com/rust-lang/rust/issues/127343>
625+
// FIXME: we should figure out a more generic version of doing this, ideally in cargo itself.
626+
fn detect_old_time_crate_version(
627+
&self,
628+
span: Option<Span>,
629+
kind: &InferSourceKind<'_>,
630+
// We will clear the non-actionable suggestion from the error to reduce noise.
631+
infer_subdiags: &mut Vec<SourceKindSubdiag<'_>>,
632+
) -> bool {
633+
// FIXME(#129461): We are time-boxing this code in the compiler. It'll start failing
634+
// compilation once we promote 1.89 to beta, which will happen in 9 months from now.
635+
#[cfg(not(version("1.89")))]
636+
const fn version_check() {}
637+
#[cfg(version("1.89"))]
638+
const fn version_check() {
639+
panic!("remove this check as presumably the ecosystem has moved from needing it");
640+
}
641+
const { version_check() };
642+
// Only relevant when building the `time` crate.
643+
if self.infcx.tcx.crate_name(LOCAL_CRATE) == sym::time
644+
&& let Some(span) = span
645+
&& let InferSourceKind::LetBinding { pattern_name, .. } = kind
646+
&& let Some(name) = pattern_name
647+
&& name.as_str() == "items"
648+
&& let FileName::Real(file) = self.infcx.tcx.sess.source_map().span_to_filename(span)
649+
{
650+
let path = file.local_path_if_available().to_string_lossy();
651+
if path.contains("format_description") && path.contains("parse") {
652+
infer_subdiags.clear();
653+
return true;
654+
}
655+
}
656+
false
657+
}
616658
}
617659

618660
#[derive(Debug)]

compiler/rustc_trait_selection/src/errors.rs

+2
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ pub struct AnnotationRequired<'a> {
205205
#[note(trait_selection_full_type_written)]
206206
pub was_written: bool,
207207
pub path: PathBuf,
208+
#[note(trait_selection_type_annotations_needed_error_time)]
209+
pub time_version: bool,
208210
}
209211

210212
// Copy of `AnnotationRequired` for E0283

compiler/rustc_trait_selection/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#![feature(assert_matches)]
2020
#![feature(associated_type_defaults)]
2121
#![feature(box_patterns)]
22+
#![feature(cfg_version)]
2223
#![feature(control_flow_enum)]
2324
#![feature(extract_if)]
2425
#![feature(if_let_guard)]
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)