Skip to content

Commit fcb34b1

Browse files
Pass the target crate in HirFormatter
This is required to format evaluated consts, because we need trait env, and it needs the crate (currently it uses the last crate in topological order, which is wrong, the next commit will fix that).
1 parent 860637a commit fcb34b1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+824
-499
lines changed

Diff for: src/tools/rust-analyzer/crates/hir-ty/src/consteval.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ use stdx::never;
1515
use triomphe::Arc;
1616

1717
use crate::{
18-
db::HirDatabase, generics::Generics, infer::InferenceContext, lower::ParamLoweringMode,
19-
mir::monomorphize_mir_body_bad, to_placeholder_idx, Const, ConstData, ConstScalar, ConstValue,
20-
GenericArg, Interner, MemoryMap, Substitution, TraitEnvironment, Ty, TyBuilder,
18+
db::HirDatabase, display::DisplayTarget, generics::Generics, infer::InferenceContext,
19+
lower::ParamLoweringMode, mir::monomorphize_mir_body_bad, to_placeholder_idx, Const, ConstData,
20+
ConstScalar, ConstValue, GenericArg, Interner, MemoryMap, Substitution, TraitEnvironment, Ty,
21+
TyBuilder,
2122
};
2223

2324
use super::mir::{interpret_mir, lower_to_mir, pad16, MirEvalError, MirLowerError};
@@ -62,11 +63,15 @@ impl ConstEvalError {
6263
f: &mut String,
6364
db: &dyn HirDatabase,
6465
span_formatter: impl Fn(span::FileId, span::TextRange) -> String,
65-
edition: span::Edition,
66+
display_target: DisplayTarget,
6667
) -> std::result::Result<(), std::fmt::Error> {
6768
match self {
68-
ConstEvalError::MirLowerError(e) => e.pretty_print(f, db, span_formatter, edition),
69-
ConstEvalError::MirEvalError(e) => e.pretty_print(f, db, span_formatter, edition),
69+
ConstEvalError::MirLowerError(e) => {
70+
e.pretty_print(f, db, span_formatter, display_target)
71+
}
72+
ConstEvalError::MirEvalError(e) => {
73+
e.pretty_print(f, db, span_formatter, display_target)
74+
}
7075
}
7176
}
7277
}

Diff for: src/tools/rust-analyzer/crates/hir-ty/src/consteval/tests.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use test_fixture::WithFixture;
1010
use test_utils::skip_slow_tests;
1111

1212
use crate::{
13-
consteval::try_const_usize, db::HirDatabase, mir::pad16, test_db::TestDB, Const, ConstScalar,
14-
Interner, MemoryMap,
13+
consteval::try_const_usize, db::HirDatabase, display::DisplayTarget, mir::pad16,
14+
test_db::TestDB, Const, ConstScalar, Interner, MemoryMap,
1515
};
1616

1717
use super::{
@@ -101,11 +101,17 @@ fn check_answer(
101101
fn pretty_print_err(e: ConstEvalError, db: TestDB) -> String {
102102
let mut err = String::new();
103103
let span_formatter = |file, range| format!("{file:?} {range:?}");
104-
let edition =
105-
db.crate_graph()[*db.crate_graph().crates_in_topological_order().last().unwrap()].edition;
104+
let display_target = DisplayTarget::from_crate(
105+
&db,
106+
*db.crate_graph().crates_in_topological_order().last().unwrap(),
107+
);
106108
match e {
107-
ConstEvalError::MirLowerError(e) => e.pretty_print(&mut err, &db, span_formatter, edition),
108-
ConstEvalError::MirEvalError(e) => e.pretty_print(&mut err, &db, span_formatter, edition),
109+
ConstEvalError::MirLowerError(e) => {
110+
e.pretty_print(&mut err, &db, span_formatter, display_target)
111+
}
112+
ConstEvalError::MirEvalError(e) => {
113+
e.pretty_print(&mut err, &db, span_formatter, display_target)
114+
}
109115
}
110116
.unwrap();
111117
err

Diff for: src/tools/rust-analyzer/crates/hir-ty/src/diagnostics/expr.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use intern::sym;
1616
use itertools::Itertools;
1717
use rustc_hash::FxHashSet;
1818
use rustc_pattern_analysis::constructor::Constructor;
19-
use span::Edition;
2019
use syntax::{
2120
ast::{self, UnaryOp},
2221
AstNode,
@@ -31,7 +30,7 @@ use crate::{
3130
self,
3231
pat_analysis::{self, DeconstructedPat, MatchCheckCtx, WitnessPat},
3332
},
34-
display::HirDisplay,
33+
display::{DisplayTarget, HirDisplay},
3534
Adjust, InferenceResult, Interner, Ty, TyExt, TyKind,
3635
};
3736

@@ -633,24 +632,24 @@ fn missing_match_arms<'p>(
633632
arms_is_empty: bool,
634633
krate: CrateId,
635634
) -> String {
636-
struct DisplayWitness<'a, 'p>(&'a WitnessPat<'p>, &'a MatchCheckCtx<'p>, Edition);
635+
struct DisplayWitness<'a, 'p>(&'a WitnessPat<'p>, &'a MatchCheckCtx<'p>, DisplayTarget);
637636
impl fmt::Display for DisplayWitness<'_, '_> {
638637
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
639-
let DisplayWitness(witness, cx, edition) = *self;
638+
let DisplayWitness(witness, cx, display_target) = *self;
640639
let pat = cx.hoist_witness_pat(witness);
641-
write!(f, "{}", pat.display(cx.db, edition))
640+
write!(f, "{}", pat.display(cx.db, display_target))
642641
}
643642
}
644643

645-
let edition = cx.db.crate_graph()[krate].edition;
646644
let non_empty_enum = match scrut_ty.as_adt() {
647645
Some((AdtId::EnumId(e), _)) => !cx.db.enum_data(e).variants.is_empty(),
648646
_ => false,
649647
};
648+
let display_target = DisplayTarget::from_crate(cx.db, krate);
650649
if arms_is_empty && !non_empty_enum {
651-
format!("type `{}` is non-empty", scrut_ty.display(cx.db, edition))
650+
format!("type `{}` is non-empty", scrut_ty.display(cx.db, display_target))
652651
} else {
653-
let pat_display = |witness| DisplayWitness(witness, cx, edition);
652+
let pat_display = |witness| DisplayWitness(witness, cx, display_target);
654653
const LIMIT: usize = 3;
655654
match &*witnesses {
656655
[witness] => format!("`{}` not covered", pat_display(witness)),

0 commit comments

Comments
 (0)