From fe7bf993aa8d64668707e348f2ea69918cfda9a4 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Fri, 8 May 2020 17:36:11 +0200 Subject: Implement better handling of divergence Divergence here means that for some reason, the end of a block will not be reached. We tried to model this just using the never type, but that doesn't work fully (e.g. in `let x = { loop {}; "foo" };` x should still have type `&str`); so this introduces a `diverges` flag that the type checker keeps track of, like rustc does. --- crates/ra_hir_ty/src/lib.rs | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'crates/ra_hir_ty/src/lib.rs') diff --git a/crates/ra_hir_ty/src/lib.rs b/crates/ra_hir_ty/src/lib.rs index a6f56c661..ac0ef1bfe 100644 --- a/crates/ra_hir_ty/src/lib.rs +++ b/crates/ra_hir_ty/src/lib.rs @@ -730,6 +730,13 @@ impl Ty { } } + pub fn is_never(&self) -> bool { + match self { + Ty::Apply(ApplicationTy { ctor: TypeCtor::Never, .. }) => true, + _ => false, + } + } + /// If this is a `dyn Trait` type, this returns the `Trait` part. pub fn dyn_trait_ref(&self) -> Option<&TraitRef> { match self { -- cgit v1.2.3 From f8bf94a4b94074eb344e495dfb4dab4bec6bc20e Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Fri, 8 May 2020 19:30:02 +0200 Subject: Use matches! --- crates/ra_hir_ty/src/lib.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'crates/ra_hir_ty/src/lib.rs') diff --git a/crates/ra_hir_ty/src/lib.rs b/crates/ra_hir_ty/src/lib.rs index ac0ef1bfe..3e5f38d0d 100644 --- a/crates/ra_hir_ty/src/lib.rs +++ b/crates/ra_hir_ty/src/lib.rs @@ -731,10 +731,7 @@ impl Ty { } pub fn is_never(&self) -> bool { - match self { - Ty::Apply(ApplicationTy { ctor: TypeCtor::Never, .. }) => true, - _ => false, - } + matches!(self, Ty::Apply(ApplicationTy { ctor: TypeCtor::Never, .. })) } /// If this is a `dyn Trait` type, this returns the `Trait` part. -- cgit v1.2.3 From a3d866e776f43c1ae717740bf0c507f4d9fe47cb Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Fri, 8 May 2020 22:12:16 +0200 Subject: Handle coercing function types to function pointers in match E.g. in ```rust match x { 1 => function1, 2 => function2, } ``` we need to try coercing both to pointers. Turns out this is a special case in rustc as well (see the link in the comment). --- crates/ra_hir_ty/src/lib.rs | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'crates/ra_hir_ty/src/lib.rs') diff --git a/crates/ra_hir_ty/src/lib.rs b/crates/ra_hir_ty/src/lib.rs index 3e5f38d0d..e8f3482fe 100644 --- a/crates/ra_hir_ty/src/lib.rs +++ b/crates/ra_hir_ty/src/lib.rs @@ -683,6 +683,12 @@ impl Ty { pub fn unit() -> Self { Ty::apply(TypeCtor::Tuple { cardinality: 0 }, Substs::empty()) } + pub fn fn_ptr(sig: FnSig) -> Self { + Ty::apply( + TypeCtor::FnPtr { num_args: sig.params().len() as u16 }, + Substs(sig.params_and_return), + ) + } pub fn as_reference(&self) -> Option<(&Ty, Mutability)> { match self { -- cgit v1.2.3