about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorChayim Refael Friedman <chayimfr@gmail.com>2025-02-04 23:07:05 +0200
committerChayim Refael Friedman <chayimfr@gmail.com>2025-02-04 23:07:05 +0200
commitcee2df27b7a1ae72cecad3d1d4d16911db1e596d (patch)
tree31e1e0d94acab9f5cb3a07ce358f0befe4852352 /src
parentd9587198f13cc23d755640a883bc4161be21c886 (diff)
downloadrust-cee2df27b7a1ae72cecad3d1d4d16911db1e596d.tar.gz
rust-cee2df27b7a1ae72cecad3d1d4d16911db1e596d.zip
Refactor `TyLoweringContext::substs_from_args_and_bindings()` to always take a `GenericDefId`
Diffstat (limited to 'src')
-rw-r--r--src/tools/rust-analyzer/crates/hir-ty/src/infer.rs3
-rw-r--r--src/tools/rust-analyzer/crates/hir-ty/src/lower.rs42
2 files changed, 18 insertions, 27 deletions
diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/infer.rs b/src/tools/rust-analyzer/crates/hir-ty/src/infer.rs
index 617ebba8811..cf6905a0b28 100644
--- a/src/tools/rust-analyzer/crates/hir-ty/src/infer.rs
+++ b/src/tools/rust-analyzer/crates/hir-ty/src/infer.rs
@@ -1625,8 +1625,7 @@ impl<'a> InferenceContext<'a> {
                     None => path.segments().last().unwrap(),
                     Some(n) => path.segments().get(path.segments().len() - n - 1).unwrap(),
                 };
-                let substs =
-                    ctx.substs_from_path_segment(resolved_seg, Some(it.into()), true, None);
+                let substs = ctx.substs_from_path_segment(resolved_seg, it.into(), true, None);
                 drop(ctx);
                 let ty = self.db.ty(it.into());
                 let ty = self.insert_type_vars(ty.substitute(Interner, &substs));
diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/lower.rs b/src/tools/rust-analyzer/crates/hir-ty/src/lower.rs
index db13e1fd354..595929a8f41 100644
--- a/src/tools/rust-analyzer/crates/hir-ty/src/lower.rs
+++ b/src/tools/rust-analyzer/crates/hir-ty/src/lower.rs
@@ -592,7 +592,7 @@ impl<'a> TyLoweringContext<'a> {
                                 // this point (`trait_ref.substitution`).
                                 let substitution = self.substs_from_path_segment(
                                     segment,
-                                    Some(associated_ty.into()),
+                                    associated_ty.into(),
                                     false,
                                     None,
                                 );
@@ -1007,7 +1007,7 @@ impl<'a> TyLoweringContext<'a> {
                 // that method to optionally take parent `Substitution` as we already know them at
                 // this point (`t.substitution`).
                 let substs =
-                    self.substs_from_path_segment(segment, Some(associated_ty.into()), false, None);
+                    self.substs_from_path_segment(segment, associated_ty.into(), false, None);
 
                 let len_self =
                     crate::generics::generics(self.db.upcast(), associated_ty.into()).len_self();
@@ -1037,9 +1037,9 @@ impl<'a> TyLoweringContext<'a> {
         infer_args: bool,
     ) -> Ty {
         let generic_def = match typeable {
-            TyDefId::BuiltinType(_) => None,
-            TyDefId::AdtId(it) => Some(it.into()),
-            TyDefId::TypeAliasId(it) => Some(it.into()),
+            TyDefId::BuiltinType(builtin) => return TyBuilder::builtin(builtin),
+            TyDefId::AdtId(it) => it.into(),
+            TyDefId::TypeAliasId(it) => it.into(),
         };
         let substs = self.substs_from_path_segment(segment, generic_def, infer_args, None);
         self.db.ty(typeable).substitute(Interner, &substs)
@@ -1058,11 +1058,11 @@ impl<'a> TyLoweringContext<'a> {
     ) -> Substitution {
         let last = path.segments().last();
         let (segment, generic_def) = match resolved {
-            ValueTyDefId::FunctionId(it) => (last, Some(it.into())),
-            ValueTyDefId::StructId(it) => (last, Some(it.into())),
-            ValueTyDefId::UnionId(it) => (last, Some(it.into())),
-            ValueTyDefId::ConstId(it) => (last, Some(it.into())),
-            ValueTyDefId::StaticId(_) => (last, None),
+            ValueTyDefId::FunctionId(it) => (last, it.into()),
+            ValueTyDefId::StructId(it) => (last, it.into()),
+            ValueTyDefId::UnionId(it) => (last, it.into()),
+            ValueTyDefId::ConstId(it) => (last, it.into()),
+            ValueTyDefId::StaticId(_) => return Substitution::empty(Interner),
             ValueTyDefId::EnumVariantId(var) => {
                 // the generic args for an enum variant may be either specified
                 // on the segment referring to the enum, or on the segment
@@ -1075,23 +1075,17 @@ impl<'a> TyLoweringContext<'a> {
                     Some(segment) if segment.args_and_bindings.is_some() => Some(segment),
                     _ => last,
                 };
-                (segment, Some(var.lookup(self.db.upcast()).parent.into()))
+                (segment, var.lookup(self.db.upcast()).parent.into())
             }
         };
-        if let Some(segment) = segment {
-            self.substs_from_path_segment(segment, generic_def, infer_args, None)
-        } else if let Some(generic_def) = generic_def {
-            // lang item
-            self.substs_from_args_and_bindings(None, Some(generic_def), infer_args, None)
-        } else {
-            Substitution::empty(Interner)
-        }
+        let args_and_bindings = segment.and_then(|it| it.args_and_bindings);
+        self.substs_from_args_and_bindings(args_and_bindings, generic_def, infer_args, None)
     }
 
     pub(super) fn substs_from_path_segment(
         &mut self,
         segment: PathSegment<'_>,
-        def: Option<GenericDefId>,
+        def: GenericDefId,
         infer_args: bool,
         explicit_self_ty: Option<Ty>,
     ) -> Substitution {
@@ -1106,12 +1100,10 @@ impl<'a> TyLoweringContext<'a> {
     fn substs_from_args_and_bindings(
         &mut self,
         args_and_bindings: Option<&GenericArgs>,
-        def: Option<GenericDefId>,
+        def: GenericDefId,
         infer_args: bool,
         explicit_self_ty: Option<Ty>,
     ) -> Substitution {
-        let Some(def) = def else { return Substitution::empty(Interner) };
-
         // Order is
         // - Optional Self parameter
         // - Lifetime parameters
@@ -1317,7 +1309,7 @@ impl<'a> TyLoweringContext<'a> {
         resolved: TraitId,
         explicit_self_ty: Ty,
     ) -> Substitution {
-        self.substs_from_path_segment(segment, Some(resolved.into()), false, Some(explicit_self_ty))
+        self.substs_from_path_segment(segment, resolved.into(), false, Some(explicit_self_ty))
     }
 
     pub(crate) fn lower_where_predicate<'b>(
@@ -1445,7 +1437,7 @@ impl<'a> TyLoweringContext<'a> {
                 let substitution = self.substs_from_path_segment(
                     // FIXME: This is hack. We shouldn't really build `PathSegment` directly.
                     PathSegment { name: &binding.name, args_and_bindings: binding.args.as_ref() },
-                    Some(associated_ty.into()),
+                    associated_ty.into(),
                     false, // this is not relevant
                     Some(super_trait_ref.self_type_parameter(Interner)),
                 );