Skip to content

Commit 835972b

Browse files
committed
Auto merge of rust-lang#18068 - Veykril:ty-fixes, r=Veykril
fix: Always explicitly set `TraitRef` self types when lowering
2 parents 07544c5 + 2c8e24e commit 835972b

File tree

11 files changed

+87
-71
lines changed

11 files changed

+87
-71
lines changed

src/tools/rust-analyzer/Cargo.lock

+6-6
Original file line numberDiff line numberDiff line change
@@ -997,24 +997,24 @@ checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24"
997997

998998
[[package]]
999999
name = "lsp-server"
1000-
version = "0.7.6"
1001-
source = "registry+https://github.com/rust-lang/crates.io-index"
1002-
checksum = "248f65b78f6db5d8e1b1604b4098a28b43d21a8eb1deeca22b1c421b276c7095"
1000+
version = "0.7.7"
10031001
dependencies = [
10041002
"crossbeam-channel",
1003+
"ctrlc",
10051004
"log",
1005+
"lsp-types",
10061006
"serde",
10071007
"serde_json",
10081008
]
10091009

10101010
[[package]]
10111011
name = "lsp-server"
10121012
version = "0.7.7"
1013+
source = "registry+https://github.com/rust-lang/crates.io-index"
1014+
checksum = "550446e84739dcaf6d48a4a093973850669e13e8a34d8f8d64851041be267cd9"
10131015
dependencies = [
10141016
"crossbeam-channel",
1015-
"ctrlc",
10161017
"log",
1017-
"lsp-types",
10181018
"serde",
10191019
"serde_json",
10201020
]
@@ -1652,7 +1652,7 @@ dependencies = [
16521652
"intern",
16531653
"itertools",
16541654
"load-cargo",
1655-
"lsp-server 0.7.6",
1655+
"lsp-server 0.7.7 (registry+https://github.com/rust-lang/crates.io-index)",
16561656
"lsp-types",
16571657
"memchr",
16581658
"mimalloc",

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

+15-15
Original file line numberDiff line numberDiff line change
@@ -1556,7 +1556,7 @@ fn builtin_derive_macro() {
15561556
Bar,
15571557
}
15581558
#[derive(Clone)]
1559-
struct X(i32, Z, i64)
1559+
struct X(i32, Z, i64);
15601560
#[derive(Clone)]
15611561
struct Y {
15621562
field1: i32,
@@ -1574,20 +1574,20 @@ fn builtin_derive_macro() {
15741574
);
15751575
check_number(
15761576
r#"
1577-
//- minicore: default, derive, builtin_impls
1578-
#[derive(Default)]
1579-
struct X(i32, Y, i64)
1580-
#[derive(Default)]
1581-
struct Y {
1582-
field1: i32,
1583-
field2: u8,
1584-
}
1577+
//- minicore: default, derive, builtin_impls
1578+
#[derive(Default)]
1579+
struct X(i32, Y, i64);
1580+
#[derive(Default)]
1581+
struct Y {
1582+
field1: i32,
1583+
field2: u8,
1584+
}
15851585
1586-
const GOAL: u8 = {
1587-
let x = X::default();
1588-
x.1.field2
1589-
};
1590-
"#,
1586+
const GOAL: u8 = {
1587+
let x = X::default();
1588+
x.1.field2
1589+
};
1590+
"#,
15911591
0,
15921592
);
15931593
}
@@ -2828,7 +2828,7 @@ fn type_error() {
28282828
y.0
28292829
};
28302830
"#,
2831-
|e| matches!(e, ConstEvalError::MirLowerError(MirLowerError::TypeMismatch(_))),
2831+
|e| matches!(e, ConstEvalError::MirLowerError(MirLowerError::HasErrors)),
28322832
);
28332833
}
28342834

src/tools/rust-analyzer/crates/hir-ty/src/infer/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,7 @@ impl InferenceContext<'_> {
851851
};
852852

853853
for (expr, ty) in exprs.iter().zip(tys.iter_mut()) {
854-
self.infer_expr_coerce(*expr, &Expectation::has_type(ty.clone()));
854+
*ty = self.infer_expr_coerce(*expr, &Expectation::has_type(ty.clone()));
855855
}
856856

857857
TyKind::Tuple(tys.len(), Substitution::from_iter(Interner, tys)).intern(Interner)

src/tools/rust-analyzer/crates/hir-ty/src/infer/path.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,12 @@ impl InferenceContext<'_> {
247247
&self.resolver,
248248
self.owner.into(),
249249
);
250-
let trait_ref =
251-
ctx.lower_trait_ref_from_resolved_path(trait_, resolved_segment, None);
250+
let trait_ref = ctx.lower_trait_ref_from_resolved_path(
251+
trait_,
252+
resolved_segment,
253+
self.table.new_type_var(),
254+
);
255+
252256
self.resolve_trait_assoc_item(trait_ref, segment, id)
253257
}
254258
(def, _) => {

src/tools/rust-analyzer/crates/hir-ty/src/lower.rs

+22-27
Original file line numberDiff line numberDiff line change
@@ -516,8 +516,11 @@ impl<'a> TyLoweringContext<'a> {
516516
TypeNs::TraitId(trait_) => {
517517
let ty = match remaining_segments.len() {
518518
1 => {
519-
let trait_ref =
520-
self.lower_trait_ref_from_resolved_path(trait_, resolved_segment, None);
519+
let trait_ref = self.lower_trait_ref_from_resolved_path(
520+
trait_,
521+
resolved_segment,
522+
TyKind::Error.intern(Interner),
523+
);
521524
let segment = remaining_segments.first().unwrap();
522525
let found = self
523526
.db
@@ -952,11 +955,17 @@ impl<'a> TyLoweringContext<'a> {
952955
Substitution::from_iter(Interner, substs)
953956
}
954957

955-
fn lower_trait_ref_from_path(
958+
pub(crate) fn lower_trait_ref_from_resolved_path(
956959
&self,
957-
path: &Path,
958-
explicit_self_ty: Option<Ty>,
959-
) -> Option<TraitRef> {
960+
resolved: TraitId,
961+
segment: PathSegment<'_>,
962+
explicit_self_ty: Ty,
963+
) -> TraitRef {
964+
let substs = self.trait_ref_substs_from_path(segment, resolved, explicit_self_ty);
965+
TraitRef { trait_id: to_chalk_trait_id(resolved), substitution: substs }
966+
}
967+
968+
fn lower_trait_ref_from_path(&self, path: &Path, explicit_self_ty: Ty) -> Option<TraitRef> {
960969
let resolved = match self.resolver.resolve_path_in_type_ns_fully(self.db.upcast(), path)? {
961970
// FIXME(trait_alias): We need to handle trait alias here.
962971
TypeNs::TraitId(tr) => tr,
@@ -966,31 +975,17 @@ impl<'a> TyLoweringContext<'a> {
966975
Some(self.lower_trait_ref_from_resolved_path(resolved, segment, explicit_self_ty))
967976
}
968977

969-
pub(crate) fn lower_trait_ref_from_resolved_path(
970-
&self,
971-
resolved: TraitId,
972-
segment: PathSegment<'_>,
973-
explicit_self_ty: Option<Ty>,
974-
) -> TraitRef {
975-
let substs = self.trait_ref_substs_from_path(segment, resolved, explicit_self_ty);
976-
TraitRef { trait_id: to_chalk_trait_id(resolved), substitution: substs }
977-
}
978-
979-
fn lower_trait_ref(
980-
&self,
981-
trait_ref: &HirTraitRef,
982-
explicit_self_ty: Option<Ty>,
983-
) -> Option<TraitRef> {
978+
fn lower_trait_ref(&self, trait_ref: &HirTraitRef, explicit_self_ty: Ty) -> Option<TraitRef> {
984979
self.lower_trait_ref_from_path(&trait_ref.path, explicit_self_ty)
985980
}
986981

987982
fn trait_ref_substs_from_path(
988983
&self,
989984
segment: PathSegment<'_>,
990985
resolved: TraitId,
991-
explicit_self_ty: Option<Ty>,
986+
explicit_self_ty: Ty,
992987
) -> Substitution {
993-
self.substs_from_path_segment(segment, Some(resolved.into()), false, explicit_self_ty)
988+
self.substs_from_path_segment(segment, Some(resolved.into()), false, Some(explicit_self_ty))
994989
}
995990

996991
pub(crate) fn lower_where_predicate<'b>(
@@ -1041,7 +1036,7 @@ impl<'a> TyLoweringContext<'a> {
10411036
let mut trait_ref = None;
10421037
let clause = match bound.as_ref() {
10431038
TypeBound::Path(path, TraitBoundModifier::None) => {
1044-
trait_ref = self.lower_trait_ref_from_path(path, Some(self_ty));
1039+
trait_ref = self.lower_trait_ref_from_path(path, self_ty);
10451040
trait_ref.clone().map(WhereClause::Implemented).map(crate::wrap_empty_binders)
10461041
}
10471042
TypeBound::Path(path, TraitBoundModifier::Maybe) => {
@@ -1053,7 +1048,7 @@ impl<'a> TyLoweringContext<'a> {
10531048
// `?Sized` has no of them.
10541049
// If we got another trait here ignore the bound completely.
10551050
let trait_id = self
1056-
.lower_trait_ref_from_path(path, Some(self_ty.clone()))
1051+
.lower_trait_ref_from_path(path, self_ty.clone())
10571052
.map(|trait_ref| trait_ref.hir_trait_id());
10581053
if trait_id == sized_trait {
10591054
self.unsized_types.borrow_mut().insert(self_ty);
@@ -1062,7 +1057,7 @@ impl<'a> TyLoweringContext<'a> {
10621057
}
10631058
TypeBound::ForLifetime(_, path) => {
10641059
// FIXME Don't silently drop the hrtb lifetimes here
1065-
trait_ref = self.lower_trait_ref_from_path(path, Some(self_ty));
1060+
trait_ref = self.lower_trait_ref_from_path(path, self_ty);
10661061
trait_ref.clone().map(WhereClause::Implemented).map(crate::wrap_empty_binders)
10671062
}
10681063
TypeBound::Lifetime(l) => {
@@ -2126,7 +2121,7 @@ pub(crate) fn impl_trait_query(db: &dyn HirDatabase, impl_id: ImplId) -> Option<
21262121
.with_type_param_mode(ParamLoweringMode::Variable);
21272122
let (self_ty, binders) = db.impl_self_ty(impl_id).into_value_and_skipped_binders();
21282123
let target_trait = impl_data.target_trait.as_ref()?;
2129-
Some(Binders::new(binders, ctx.lower_trait_ref(target_trait, Some(self_ty))?))
2124+
Some(Binders::new(binders, ctx.lower_trait_ref(target_trait, self_ty)?))
21302125
}
21312126

21322127
pub(crate) fn return_type_impl_traits(

src/tools/rust-analyzer/crates/hir-ty/src/mir/eval.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -421,9 +421,25 @@ impl MirEvalError {
421421
}
422422
MirEvalError::MirLowerError(func, err) => {
423423
let function_name = db.function_data(*func);
424+
let self_ = match func.lookup(db.upcast()).container {
425+
ItemContainerId::ImplId(impl_id) => Some({
426+
let generics = crate::generics::generics(db.upcast(), impl_id.into());
427+
let substs = generics.placeholder_subst(db);
428+
db.impl_self_ty(impl_id)
429+
.substitute(Interner, &substs)
430+
.display(db, edition)
431+
.to_string()
432+
}),
433+
ItemContainerId::TraitId(it) => {
434+
Some(db.trait_data(it).name.display(db.upcast(), edition).to_string())
435+
}
436+
_ => None,
437+
};
424438
writeln!(
425439
f,
426-
"MIR lowering for function `{}` ({:?}) failed due:",
440+
"MIR lowering for function `{}{}{}` ({:?}) failed due:",
441+
self_.as_deref().unwrap_or_default(),
442+
if self_.is_some() { "::" } else { "" },
427443
function_name.name.display(db.upcast(), edition),
428444
func
429445
)?;

src/tools/rust-analyzer/crates/hir-ty/src/mir/lower.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ pub enum MirLowerError {
9494
UnresolvedField,
9595
UnsizedTemporary(Ty),
9696
MissingFunctionDefinition(DefWithBodyId, ExprId),
97-
TypeMismatch(Option<TypeMismatch>),
97+
TypeMismatch(TypeMismatch),
98+
HasErrors,
9899
/// This should never happen. Type mismatch should catch everything.
99100
TypeError(&'static str),
100101
NotSupported(String),
@@ -179,15 +180,13 @@ impl MirLowerError {
179180
body.pretty_print_expr(db.upcast(), *owner, *it, edition)
180181
)?;
181182
}
182-
MirLowerError::TypeMismatch(e) => match e {
183-
Some(e) => writeln!(
184-
f,
185-
"Type mismatch: Expected {}, found {}",
186-
e.expected.display(db, edition),
187-
e.actual.display(db, edition),
188-
)?,
189-
None => writeln!(f, "Type mismatch: types mismatch with {{unknown}}",)?,
190-
},
183+
MirLowerError::HasErrors => writeln!(f, "Type inference result contains errors")?,
184+
MirLowerError::TypeMismatch(e) => writeln!(
185+
f,
186+
"Type mismatch: Expected {}, found {}",
187+
e.expected.display(db, edition),
188+
e.actual.display(db, edition),
189+
)?,
191190
MirLowerError::GenericArgNotProvided(id, subst) => {
192191
let parent = id.parent;
193192
let param = &db.generic_params(parent)[id.local_id];
@@ -2184,7 +2183,7 @@ pub fn lower_to_mir(
21842183
root_expr: ExprId,
21852184
) -> Result<MirBody> {
21862185
if infer.has_errors {
2187-
return Err(MirLowerError::TypeMismatch(None));
2186+
return Err(MirLowerError::HasErrors);
21882187
}
21892188
let mut ctx = MirLowerCtx::new(db, owner, body, infer);
21902189
// 0 is return local

src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/invalid_cast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ fn main() {
831831
//- minicore: sized
832832
fn main() {
833833
_ = ((), ()) as ();
834-
//^^^^^^^^^^^^^^ error: non-primitive cast: `(_, _)` as `()`
834+
//^^^^^^^^^^^^^^ error: non-primitive cast: `((), ())` as `()`
835835
}
836836
"#,
837837
);

src/tools/rust-analyzer/crates/rust-analyzer/src/global_state.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ impl GlobalState {
540540
}
541541

542542
pub(crate) fn respond(&mut self, response: lsp_server::Response) {
543-
if let Some((method, start)) = self.req_queue.incoming.complete(response.id.clone()) {
543+
if let Some((method, start)) = self.req_queue.incoming.complete(&response.id) {
544544
if let Some(err) = &response.error {
545545
if err.message.starts_with("server panicked") {
546546
self.poke_rust_analyzer_developer(format!("{}, check the log", err.message))

src/tools/rust-analyzer/crates/test-fixture/src/lib.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,10 @@ pub trait WithFixture: Default + ExpandDatabase + SourceRootDatabase + 'static {
9595
fn test_crate(&self) -> CrateId {
9696
let crate_graph = self.crate_graph();
9797
let mut it = crate_graph.iter();
98-
let res = it.next().unwrap();
99-
assert!(it.next().is_none());
98+
let mut res = it.next().unwrap();
99+
while crate_graph[res].origin.is_lang() {
100+
res = it.next().unwrap();
101+
}
100102
res
101103
}
102104
}

src/tools/rust-analyzer/crates/test-utils/src/minicore.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ pub mod default {
171171
macro_rules! impl_default {
172172
($v:literal; $($t:ty)*) => {
173173
$(
174-
impl const Default for $t {
174+
impl Default for $t {
175175
fn default() -> Self {
176176
$v
177177
}
@@ -686,7 +686,7 @@ pub mod ops {
686686
// endregion:fn
687687
// region:try
688688
mod try_ {
689-
use super::super::convert::Infallible;
689+
use crate::convert::Infallible;
690690

691691
pub enum ControlFlow<B, C = ()> {
692692
#[lang = "Continue"]
@@ -756,7 +756,7 @@ pub mod ops {
756756
// endregion:option
757757
// region:result
758758
// region:from
759-
use super::super::convert::From;
759+
use crate::convert::From;
760760

761761
impl<T, E> Try for Result<T, E> {
762762
type Output = T;
@@ -777,7 +777,7 @@ pub mod ops {
777777
impl<T, E, F: From<E>> FromResidual<Result<Infallible, E>> for Result<T, F> {
778778
fn from_residual(residual: Result<Infallible, E>) -> Self {
779779
match residual {
780-
Err(e) => Err(From::from(e)),
780+
Err(e) => Err(F::from(e)),
781781
Ok(_) => loop {},
782782
}
783783
}

0 commit comments

Comments
 (0)