Skip to content

Commit

Permalink
Emit specific message for time<=0.3.35
Browse files Browse the repository at this point in the history
```
error[E0282]: type annotations needed for `Box<_>`
  --> /home/gh-estebank/.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 `time` caused by a change in Rust 1.80.0; update `time` to version `>=0.3.36`
```
  • Loading branch information
estebank committed Aug 21, 2024
1 parent 4d5b3b1 commit b4414a1
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1893,6 +1893,7 @@ symbols! {
three_way_compare,
thumb2,
thumb_mode: "thumb-mode",
time,
tmm_reg,
to_owned_method,
to_string,
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_trait_selection/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,8 @@ trait_selection_type_annotations_needed = {$source_kind ->
}
.label = type must be known at this point
trait_selection_type_annotations_needed_error_time = this is an inference error on `time` caused by a change in Rust 1.80.0; update `time` to version `>=0.3.36`
trait_selection_types_declared_different = these two types are declared with different lifetimes...
trait_selection_unable_to_construct_constant_value = unable to construct a constant value for the unevaluated constant {$unevaluated}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rustc_errors::codes::*;
use rustc_errors::{Diag, IntoDiagArg};
use rustc_hir as hir;
use rustc_hir::def::{CtorOf, DefKind, Namespace, Res};
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::def_id::{DefId, LocalDefId, LOCAL_CRATE};
use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::{Body, Closure, Expr, ExprKind, FnRetTy, HirId, LetStmt, LocalSource};
use rustc_middle::bug;
Expand All @@ -18,7 +18,7 @@ use rustc_middle::ty::{
TypeFoldable, TypeFolder, TypeSuperFoldable, TypeckResults,
};
use rustc_span::symbol::{sym, Ident};
use rustc_span::{BytePos, Span, DUMMY_SP};
use rustc_span::{BytePos, FileName, Span, DUMMY_SP};

use crate::error_reporting::TypeErrCtxt;
use crate::errors::{
Expand Down Expand Up @@ -384,6 +384,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
bad_label,
was_written: None,
path: Default::default(),
time_version: None,
}),
TypeAnnotationNeeded::E0283 => self.dcx().create_err(AmbiguousImpl {
span,
Expand Down Expand Up @@ -577,6 +578,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
}
}
}

let time_version = self.detect_old_time_crate_version(failure_span, &mut infer_subdiags);

match error_code {
TypeAnnotationNeeded::E0282 => self.dcx().create_err(AnnotationRequired {
span,
Expand All @@ -588,6 +592,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
bad_label: None,
was_written: path.as_ref().map(|_| ()),
path: path.unwrap_or_default(),
time_version,
}),
TypeAnnotationNeeded::E0283 => self.dcx().create_err(AmbiguousImpl {
span,
Expand All @@ -613,6 +618,57 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
}),
}
}

/// Detect the inference regression on crate `time` <= 0.3.35 and emit a more targeted error.
/// <https://github.com/rust-lang/rust/issues/127343>
// FIXME: we should figure out a more generic version of doing this, ideally in cargo itself.
fn detect_old_time_crate_version(
&self,
span: Option<Span>,
/// We will clear the non-actionable suggestion from the error to reduce noise.
infer_subdiags: &mut Vec<SourceKindSubdiag<'_>>,
) -> Option<()> {
if self.infcx.tcx.crate_name(LOCAL_CRATE) != sym::time {
// Only relevant when building the `time` crate.
return None;
}
let Some(span) = span else { return None };
let FileName::Real(file) = self.infcx.tcx.sess.source_map().span_to_filename(span) else {
return None;
};
let path = file.local_path_if_available();
let mut components = path.components();
// We will take the filename of the error and see if it is of the form
// `.../registry/src/index.crates.io-.../time-0...`, in order to detect the specific case
// we care about.
while let Some(component) = components.next() {
let std::path::Component::Normal(component) = component else {
continue;
};
if component == "registry"
&& let Some(next) = components.next()
&& let std::path::Component::Normal(next) = next
&& next == "src"
&& let Some(next) = components.next()
&& let std::path::Component::Normal(next) = next
&& next.to_string_lossy().starts_with("index.")
&& let Some(next) = components.next()
&& let std::path::Component::Normal(next) = next
&& let next = next.to_string_lossy()
&& next.starts_with("time-0.")
&& let Some(version) = next.split('-').skip(1).next()
&& let mut segments = version.split('.')
&& let Some(major) = segments.next()
&& major == "0"
&& let Some(minor) = segments.next()
&& minor == "3"
{
infer_subdiags.clear();
return Some(());
}
}
None
}
}

#[derive(Debug)]
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_trait_selection/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ pub struct AnnotationRequired<'a> {
#[note(trait_selection_full_type_written)]
pub was_written: Option<()>,
pub path: PathBuf,
#[note(trait_selection_type_annotations_needed_error_time)]
pub time_version: Option<()>,
}

// Copy of `AnnotationRequired` for E0283
Expand Down

0 comments on commit b4414a1

Please sign in to comment.