Skip to content

Commit fc0d63d

Browse files
committed
Issue 89275 fix and test
Issue 89275 fix and test Fix librustdoc OverflowError usage rust tidy run Issue 89275 fix and test
1 parent c92f10a commit fc0d63d

File tree

10 files changed

+82
-18
lines changed

10 files changed

+82
-18
lines changed

compiler/rustc_middle/src/traits/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@ pub enum SelectionError<'tcx> {
449449
TraitNotObjectSafe(DefId),
450450
NotConstEvaluatable(NotConstEvaluatable),
451451
Overflow,
452+
ErrorReporting,
452453
}
453454

454455
/// When performing resolution, it is typically the case that there

compiler/rustc_middle/src/traits/select.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -261,12 +261,18 @@ impl EvaluationResult {
261261
}
262262
}
263263

264-
/// Indicates that trait evaluation caused overflow.
264+
/// Indicates that trait evaluation caused overflow and in which pass.
265265
#[derive(Copy, Clone, Debug, PartialEq, Eq, HashStable)]
266-
pub struct OverflowError;
266+
pub enum OverflowError {
267+
Cannonical,
268+
ErrorReporting,
269+
}
267270

268271
impl<'tcx> From<OverflowError> for SelectionError<'tcx> {
269-
fn from(OverflowError: OverflowError) -> SelectionError<'tcx> {
270-
SelectionError::Overflow
272+
fn from(overflow_error: OverflowError) -> SelectionError<'tcx> {
273+
match overflow_error {
274+
OverflowError::Cannonical => SelectionError::Overflow,
275+
OverflowError::ErrorReporting => SelectionError::ErrorReporting,
276+
}
271277
}
272278
}

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,9 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
842842
Overflow => {
843843
bug!("overflow should be handled before the `report_selection_error` path");
844844
}
845+
SelectionError::ErrorReporting => {
846+
bug!("ErrorReporting Overflow should not reach `report_selection_err` call")
847+
}
845848
};
846849

847850
self.note_obligation_cause(&mut err, &obligation);

compiler/rustc_trait_selection/src/traits/query/evaluate_obligation.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,21 @@ impl<'cx, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'cx, 'tcx> {
8383
) -> EvaluationResult {
8484
match self.evaluate_obligation(obligation) {
8585
Ok(result) => result,
86-
Err(OverflowError) => {
86+
Err(OverflowError::Cannonical) => {
8787
let mut selcx = SelectionContext::with_query_mode(&self, TraitQueryMode::Standard);
88-
selcx.evaluate_root_obligation(obligation).unwrap_or_else(|r| {
89-
span_bug!(
90-
obligation.cause.span,
91-
"Overflow should be caught earlier in standard query mode: {:?}, {:?}",
92-
obligation,
93-
r,
94-
)
88+
selcx.evaluate_root_obligation(obligation).unwrap_or_else(|r| match r {
89+
OverflowError::Cannonical => {
90+
span_bug!(
91+
obligation.cause.span,
92+
"Overflow should be caught earlier in standard query mode: {:?}, {:?}",
93+
obligation,
94+
r,
95+
)
96+
}
97+
OverflowError::ErrorReporting => EvaluationResult::EvaluatedToErr,
9598
})
9699
}
100+
Err(OverflowError::ErrorReporting) => EvaluationResult::EvaluatedToErr,
97101
}
98102
}
99103
}

compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_target::spec::abi::Abi;
1313

1414
use crate::traits::coherence::Conflict;
1515
use crate::traits::{util, SelectionResult};
16-
use crate::traits::{Overflow, Unimplemented};
16+
use crate::traits::{ErrorReporting, Overflow, Unimplemented};
1717

1818
use super::BuiltinImplConditions;
1919
use super::IntercrateAmbiguityCause;
@@ -156,7 +156,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
156156
Ok(Some(EvaluatedCandidate { candidate: c, evaluation: eval }))
157157
}
158158
Ok(_) => Ok(None),
159-
Err(OverflowError) => Err(Overflow),
159+
Err(OverflowError::Cannonical) => Err(Overflow),
160+
Err(OverflowError::ErrorReporting) => Err(ErrorReporting),
160161
})
161162
.flat_map(Result::transpose)
162163
.collect::<Result<Vec<_>, _>>()?;

compiler/rustc_trait_selection/src/traits/select/mod.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ use super::ObligationCauseCode;
2020
use super::Selection;
2121
use super::SelectionResult;
2222
use super::TraitQueryMode;
23+
use super::{ErrorReporting, Overflow, SelectionError, Unimplemented};
2324
use super::{ObligationCause, PredicateObligation, TraitObligation};
24-
use super::{Overflow, SelectionError, Unimplemented};
2525

2626
use crate::infer::{InferCtxt, InferOk, TypeFreshener};
2727
use crate::traits::error_reporting::InferCtxtExt;
@@ -900,7 +900,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
900900
match self.candidate_from_obligation(stack) {
901901
Ok(Some(c)) => self.evaluate_candidate(stack, &c),
902902
Ok(None) => Ok(EvaluatedToAmbig),
903-
Err(Overflow) => Err(OverflowError),
903+
Err(Overflow) => Err(OverflowError::Cannonical),
904+
Err(ErrorReporting) => Err(OverflowError::ErrorReporting),
904905
Err(..) => Ok(EvaluatedToErr),
905906
}
906907
}
@@ -1057,10 +1058,13 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
10571058
if !self.infcx.tcx.recursion_limit().value_within_limit(depth) {
10581059
match self.query_mode {
10591060
TraitQueryMode::Standard => {
1061+
if self.infcx.is_tainted_by_errors() {
1062+
return Err(OverflowError::ErrorReporting);
1063+
}
10601064
self.infcx.report_overflow_error(error_obligation, true);
10611065
}
10621066
TraitQueryMode::Canonical => {
1063-
return Err(OverflowError);
1067+
return Err(OverflowError::Cannonical);
10641068
}
10651069
}
10661070
}

compiler/rustc_typeck/src/check/demand.rs

+1
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
140140
Err(e) => e,
141141
};
142142

143+
self.set_tainted_by_errors();
143144
let expr = expr.peel_drop_temps();
144145
let cause = self.misc(expr.span);
145146
let expr_ty = self.resolve_vars_with_obligations(checked_ty);

src/librustdoc/clean/blanket_impl.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
7878
);
7979
match infcx.evaluate_obligation(&obligation) {
8080
Ok(eval_result) if eval_result.may_apply() => {}
81-
Err(traits::OverflowError) => {}
81+
Err(traits::OverflowError::Cannonical) => {}
82+
Err(traits::OverflowError::ErrorReporting) => {}
8283
_ => {
8384
return false;
8485
}

src/test/ui/typeck/issue-89275.rs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#![recursion_limit = "5"] // To reduce noise
2+
3+
//expect mutability error when ambiguous traits are in scope
4+
//and not an overflow error on the span in the main function.
5+
6+
struct Ratio<T>(T);
7+
8+
pub trait Pow {
9+
fn pow(self) -> Self;
10+
}
11+
12+
impl<'a, T> Pow for &'a Ratio<T>
13+
where
14+
&'a T: Pow,
15+
{
16+
fn pow(self) -> Self {
17+
self
18+
}
19+
}
20+
21+
fn downcast<'a, W: ?Sized>() -> &'a W {
22+
todo!()
23+
}
24+
25+
struct Other;
26+
27+
fn main() {
28+
let other: &mut Other = downcast();//~ERROR 28:29: 28:39: mismatched types [E0308]
29+
}

src/test/ui/typeck/issue-89275.stderr

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/issue-89275.rs:28:29
3+
|
4+
LL | let other: &mut Other = downcast();
5+
| ---------- ^^^^^^^^^^ types differ in mutability
6+
| |
7+
| expected due to this
8+
|
9+
= note: expected mutable reference `&mut Other`
10+
found reference `&_`
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)