Skip to content

Commit 97f2c37

Browse files
committed
rustc: Change the return of a query's try_get
This alters the return value of the `try_get` function so the error contains a diagnostic rather than a `CycleError`. This way consumers are forced to take *some* action (else they get a bug to an un-emitted diagnostic). This action could be to emit the error itself, or in some cases delay the diagnostic as a bug and continue.
1 parent 64b0b2b commit 97f2c37

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/librustc/ty/maps.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use errors::DiagnosticBuilder;
1112
use dep_graph::{DepConstructor, DepNode, DepNodeIndex};
1213
use errors::{Diagnostic, DiagnosticBuilder};
1314
use hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
@@ -218,7 +219,9 @@ pub struct CycleError<'a, 'tcx: 'a> {
218219
}
219220

220221
impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
221-
pub fn report_cycle(self, CycleError { span, cycle }: CycleError) {
222+
pub fn report_cycle(self, CycleError { span, cycle }: CycleError)
223+
-> DiagnosticBuilder<'a>
224+
{
222225
// Subtle: release the refcell lock before invoking `describe()`
223226
// below by dropping `cycle`.
224227
let stack = cycle.to_vec();
@@ -247,8 +250,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
247250
err.note(&format!("...which then again requires {}, completing the cycle.",
248251
stack[0].1.describe(self)));
249252

250-
err.emit();
251-
});
253+
return err
254+
})
252255
}
253256

254257
fn cycle_check<F, R>(self, span: Span, query: Query<'gcx>, compute: F)
@@ -704,8 +707,11 @@ macro_rules! define_maps {
704707
}
705708

706709
pub fn try_get(tcx: TyCtxt<'a, $tcx, 'lcx>, span: Span, key: $K)
707-
-> Result<$V, CycleError<'a, $tcx>> {
708-
Self::try_get_with(tcx, span, key, Clone::clone)
710+
-> Result<$V, DiagnosticBuilder<'a>> {
711+
match Self::try_get_with(tcx, span, key, Clone::clone) {
712+
Ok(e) => Ok(e),
713+
Err(e) => Err(tcx.report_cycle(e)),
714+
}
709715
}
710716

711717
pub fn force(tcx: TyCtxt<'a, $tcx, 'lcx>, span: Span, key: $K) {
@@ -714,7 +720,7 @@ macro_rules! define_maps {
714720

715721
match Self::try_get_with(tcx, span, key, |_| ()) {
716722
Ok(()) => {}
717-
Err(e) => tcx.report_cycle(e)
723+
Err(e) => tcx.report_cycle(e).emit(),
718724
}
719725
}
720726
})*
@@ -751,8 +757,8 @@ macro_rules! define_maps {
751757
impl<'a, $tcx, 'lcx> TyCtxtAt<'a, $tcx, 'lcx> {
752758
$($(#[$attr])*
753759
pub fn $name(self, key: $K) -> $V {
754-
queries::$name::try_get(self.tcx, self.span, key).unwrap_or_else(|e| {
755-
self.report_cycle(e);
760+
queries::$name::try_get(self.tcx, self.span, key).unwrap_or_else(|mut e| {
761+
e.emit();
756762
Value::from_cycle_error(self.global_tcx())
757763
})
758764
})*

0 commit comments

Comments
 (0)