Repository: gluon-lang/gluon (rust)

Issue:
Support rust 1.70.0
While [upgrading rust to 1.70.0](https://github.com/Homebrew/homebrew-core/pull/132584), we found that fselect has some build issue with it, see the following build failure

```
thread 'rustc' panicked at 'assertion failed: !ty.needs_infer()', compiler/rustc_trait_selection/src/traits/outlives_bounds.rs:58:9
stack backtrace:
   0:        0x10bb01036 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::ha3ddc6c8acb3b41e
   1:        0x10bb8c52b - core::fmt::write::h55c7a1bdd8f56455
   2:        0x10bb1a6bc - std::io::Write::write_fmt::h27af5145c0d40b32
   3:        0x10bb00e0a - std::sys_common::backtrace::print::h5fb8c00455f3b585
   4:        0x10bb15aa0 - std::panicking::default_hook::{{closure}}::h8f62dbf924c4ee4e
   5:        0x10bb15781 - std::panicking::default_hook::h350871482efd0a0d
   Compiling clap v2.33.3
   6:        0x113da293a - rustc_driver_impl[d9a313b8f925679e]::DEFAULT_HOOK::{closure#0}::{closure#0}
   7:        0x10bb162d3 - std::panicking::rust_panic_with_hook::hbc178a4b1ac3e911
   8:        0x10bb013c3 - std::panicking::begin_panic_handler::{{closure}}::hb0394ed0e9da6eaf
   9:        0x10bb01139 - std::sys_common::backtrace::__rust_end_short_backtrace::h8199c578d0b9b873
  10:        0x10bb15ced - _rust_begin_unwind
  11:        0x10bbc8043 - core::panicking::panic_fmt::h335afe54074b123c
  12:        0x10bbc80d7 - core::panicking::panic::h679db5d92a389689
  13:        0x1178f2d9a - <rustc_infer[32fb341d6e0a7b88]::infer::InferCtxt as rustc_trait_selection[a2fd7c4595118da]::traits::outlives_bounds::InferCtxtExt>::implied_outlives_bounds
  14:        0x115dfe356 - <rustc_infer[32fb341d6e0a7b88]::infer::outlives::env::OutlivesEnvironment>::with_bounds::<core[ef37f52debf101b8]::iter::adapters::flatten::FlatMap<indexmap[51f5518122247f91]::set::IntoIter<rustc_middle[93ef92fd333526bb]::ty::Ty>, alloc[11e576ea4f8e2b32]::vec::Vec<rustc_middle[93ef92fd333526bb]::traits::query::OutlivesBound>, <rustc_infer[32fb341d6e0a7b88]::infer::InferCtxt as rustc_trait_selection[a2fd7c4595118da]::traits::outlives_bounds::InferCtxtExt>::implied_bounds_tys::{closure#0}>>
  15:        0x115f28d6a - rustc_hir_analysis[1729db833033aa34]::check::wfcheck::check_well_formed
  16:        0x116bf1f34 - rustc_query_system[4b66a84e20c37a48]::query::plumbing::try_execute_query::<rustc_query_impl[319187f9801461b0]::queries::check_well_formed, rustc_query_impl[319187f9801461b0]::plumbing::QueryCtxt>
  17:        0x116ec97d1 - <rustc_query_impl[319187f9801461b0]::Queries as rustc_middle[93ef92fd333526bb]::ty::query::QueryEngine>::check_well_formed
  18:        0x115f1687b - rustc_data_structures[74d12b3b3d632927]::sync::par_for_each_in::<&[rustc_hir[23ec9f3d33e21551]::hir::ItemId], <rustc_middle[93ef92fd333526bb]::hir::ModuleItems>::par_items<rustc_hir_analysis[1729db833033aa34]::check::wfcheck::check_mod_type_wf::{closure#0}>::{closure#0}>
  19:        0x115f2ebb3 - rustc_hir_analysis[1729db833033aa34]::check::wfcheck::check_mod_type_wf
  20:        0x116bf16d6 - rustc_query_system[4b66a84e20c37a48]::query::plumbing::try_execute_query::<rustc_query_impl[319187f9801461b0]::queries::check_mod_type_wf, rustc_query_impl[319187f9801461b0]::plumbing::QueryCtxt>

  21:        0x116ebd3c1 - <rustc_query_impl[319187f9801461b0]::Queries as rustc_middle[93ef92fd333526bb]::ty::query::QueryEngine>::check_mod_type_wf
  22:        0x115f1696b - rustc_data_structures[74d12b3b3d632927]::sync::par_for_each_in::<&[rustc_hir[23ec9f3d33e21551]::hir_id::OwnerId], <rustc_middle[93ef92fd333526bb]::hir::map::Map>::par_for_each_module<rustc_hir_analysis[1729db833033aa34]::check_crate::{closure#5}::{closure#0}::{closure#0}>::{closure#0}>
  23:        0x115e7bf00 - <rustc_session[a38a80932245d098]::session::Session>::track_errors::<rustc_hir_analysis[1729db833033aa34]::check_crate::{closure#5}, ()>
  24:        0x115e2a864 - rustc_hir_analysis[1729db833033aa34]::check_crate
  25:        0x113eec81e - rustc_interface[22224ac49478d313]::passes::analysis
  26:        0x116c58be2 - rustc_query_system[4b66a84e20c37a48]::query::plumbing::try_execute_query::<rustc_query_impl[319187f9801461b0]::queries::analysis, rustc_query_impl[319187f9801461b0]::plumbing::QueryCtxt>
  27:        0x116eb1c79 - <rustc_query_impl[319187f9801461b0]::Queries as rustc_middle[93ef92fd333526bb]::ty::query::QueryEngine>::analysis
  28:        0x113df23a3 - <std[c074a63446a24828]::thread::local::LocalKey<core[ef37f52debf101b8]::cell::Cell<*const ()>>>::with::<rustc_middle[93ef92fd333526bb]::ty::context::tls::enter_context<<rustc_middle[93ef92fd333526bb]::ty::context::GlobalCtxt>::enter<rustc_driver_impl[d9a313b8f925679e]::run_compiler::{closure#1}::{closure#2}::{closure#4}, core[ef37f52debf101b8]::result::Result<(), rustc_span[948e26923f0f38ec]::ErrorGuaranteed>>::{closure#0}, core[ef37f52debf101b8]::result::Result<(), rustc_span[948e26923f0f38ec]::ErrorGuaranteed>>::{closure#0}, core[ef37f52debf101b8]::result::Result<(), rustc_span[948e26923f0f38ec]::ErrorGuaranteed>>
  29:        0x113db7b64 - <rustc_interface[22224ac49478d313]::queries::QueryResult<&rustc_middle[93ef92fd333526bb]::ty::context::GlobalCtxt>>::enter::<core[ef37f52debf101b8]::result::Result<(), rustc_span[948e26923f0f38ec]::ErrorGuaranteed>, rustc_driver_impl[d9a313b8f925679e]::run_compiler::{closure#1}::{closure#2}::{closure#4}>
  30:        0x113df9140 - <rustc_interface[22224ac49478d313]::interface::Compiler>::enter::<rustc_driver_impl[d9a313b8f925679e]::run_compiler::{closure#1}::{closure#2}, core[ef37f52debf101b8]::result::Result<core[ef37f52debf101b8]::option::Option<rustc_interface[22224ac49478d313]::queries::Linker>, rustc_span[948e26923f0f38ec]::ErrorGuaranteed>>
  31:        0x113da9189 - rustc_span[948e26923f0f38ec]::set_source_map::<core[ef37f52debf101b8]::result::Result<(), rustc_span[948e26923f0f38ec]::ErrorGuaranteed>, rustc_interface[22224ac49478d313]::interface::run_compiler<core[ef37f52debf101b8]::result::Result<(), rustc_span[948e26923f0f38ec]::ErrorGuaranteed>, rustc_driver_impl[d9a313b8f925679e]::run_compiler::{closure#1}>::{closure#0}::{closure#0}>
  32:        0x113db8957 - <scoped_tls[f39a6905dcd445c5]::ScopedKey<rustc_span[948e26923f0f38ec]::SessionGlobals>>::set::<rustc_interface[22224ac49478d313]::interface::run_compiler<core[ef37f52debf101b8]::result::Result<(), rustc_span[948e26923f0f38ec]::ErrorGuaranteed>, rustc_driver_impl[d9a313b8f925679e]::run_compiler::{closure#1}>::{closure#0}, core[ef37f52debf101b8]::result::Result<(), rustc_span[948e26923f0f38ec]::ErrorGuaranteed>>
  33:        0x113dae2c3 - std[c074a63446a24828]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[22224ac49478d313]::util::run_in_thread_pool_with_globals<rustc_interface[22224ac49478d313]::interface::run_compiler<core[ef37f52debf101b8]::result::Result<(), rustc_span[948e26923f0f38ec]::ErrorGuaranteed>, rustc_driver_impl[d9a313b8f925679e]::run_compiler::{closure#1}>::{closure#0}, core[ef37f52debf101b8]::result::Result<(), rustc_span[948e26923f0f38ec]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[ef37f52debf101b8]::result::Result<(), rustc_span[948e26923f0f38ec]::ErrorGuaranteed>>
  34:        0x113dae7e5 - <<std[c074a63446a24828]::thread::Builder>::spawn_unchecked_<rustc_interface[22224ac49478d313]::util::run_in_thread_pool_with_globals<rustc_interface[22224ac49478d313]::interface::run_compiler<core[ef37f52debf101b8]::result::Result<(), rustc_span[948e26923f0f38ec]::ErrorGuaranteed>, rustc_driver_impl[d9a313b8f925679e]::run_compiler::{closure#1}>::{closure#0}, core[ef37f52debf101b8]::result::Result<(), rustc_span[948e26923f0f38ec]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[ef37f52debf101b8]::result::Result<(), rustc_span[948e26923f0f38ec]::ErrorGuaranteed>>::{closure#1} as core[ef37f52debf101b8]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  35:        0x10bb02539 - std::sys::unix::thread::Thread::new::thread_start::h406ab92c130eec6c
  36:     0x7ff8136761d3 - __pthread_start

error: the compiler unexpectedly panicked. this is a bug.
```

full build log, https://github.com/Homebrew/homebrew-core/actions/runs/5164912796/jobs/9305636134
some regression test log in here as well, https://github.com/chenrui333/github-action-test/actions/runs/5269091593/jobs/9526714893

Pull request description:
chore: Compile on stable rust on CI again
The ICE has been fixed so we can restore stable compilation again.

https://github.com/rust-lang/rust/issues/112832

Closes #951

Patch:
diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml
index bac7db3c..51af20e5 100644
--- a/.github/workflows/rust.yml
+++ b/.github/workflows/rust.yml
@@ -10,7 +10,7 @@ jobs:
     runs-on: ubuntu-latest
     strategy:
       matrix:
-        rust: [nightly]  # This should be reverted back to [stable, nightly] when 1.74 is made the rustc stable version
+        rust: [stable, nightly]
     env:
       CRATE_NAME: gluon
       CARGO_INCREMENTAL: 0 # Incremental compilation is slower and bloats the cache


Test patch:
diff --git a/tests/api.rs b/tests/api.rs
index 1d121a4c..2055ef8f 100644
--- a/tests/api.rs
+++ b/tests/api.rs
@@ -16,7 +16,6 @@ use gluon::{
     query::Compilation,
     vm::{
         api::{
-            de::De,
             scoped::{Ref, RefMut},
             FunctionRef, FutureResult, Hole, OpaqueValue, OwnedFunction, RuntimeResult, VmType, IO,
         },
@@ -298,47 +297,6 @@ fn tuples_start_at_0() {
     );
 }
 
-#[test]
-fn use_type_from_type_field() {
-    let _ = ::env_logger::try_init();
-    let vm = make_vm();
-
-    #[derive(Serialize, Deserialize, PartialEq, Debug)]
-    enum Test {
-        A(i32),
-        B(String),
-        C,
-    }
-    impl VmType for Test {
-        type Type = Self;
-        fn make_type(vm: &Thread) -> ArcType {
-            if let Some(typ) = vm.get_type::<Self>() {
-                return typ;
-            }
-
-            let (name, typ) = gluon::vm::api::typ::from_rust::<Self>(vm).unwrap();
-            vm.register_type_as(
-                name.clone(),
-                Alias::new(name, Vec::new(), typ),
-                ::std::any::TypeId::of::<Self>(),
-            )
-            .unwrap()
-        }
-    }
-
-    add_extern_module(&vm, "test_types", |vm| {
-        ExternModule::new(vm, record! { type Test => Test })
-    });
-    let text = r#"
-        let { Test } = import! test_types
-        B "abc"
-    "#;
-    let (De(actual), _) = vm
-        .run_expr::<De<Test>>("test", text)
-        .unwrap_or_else(|err| panic!("{}", err));
-    assert_eq!(actual, Test::B("abc".to_string()));
-}
-
 #[test]
 fn use_rust_created_tuple_as_polymorphic() {
     let _ = ::env_logger::try_init();
@@ -368,41 +326,6 @@ fn use_rust_created_record_as_polymorphic() {
     assert_eq!(result, 1);
 }
 
-#[test]
-fn return_btreemap() {
-    let _ = ::env_logger::try_init();
-
-    let vm = make_vm();
-
-    add_extern_module_with_deps(
-        &vm,
-        "test",
-        |vm| {
-            ExternModule::new(
-                vm,
-                primitive!(1, "test", |()| {
-                    vec![("a".to_string(), 1), ("b".to_string(), 2)]
-                        .into_iter()
-                        .collect::<BTreeMap<_, _>>()
-                }),
-            )
-        },
-        vec!["std.map".into(), "std.json.de".into()],
-    );
-
-    vm.run_expr::<()>("", "let _ = import! test in ()")
-        .unwrap_or_else(|err| panic!("{}", err));
-    let (result, _) = vm
-        .run_expr::<BTreeMap<_, _>>("", "(import! test) ()")
-        .unwrap_or_else(|err| panic!("{}", err));
-    assert_eq!(
-        result,
-        vec![("a".to_string(), 1), ("b".to_string(), 2)]
-            .into_iter()
-            .collect::<BTreeMap<_, _>>()
-    );
-}
-
 #[test]
 fn get_value_boxed_or_unboxed() {
     let _ = ::env_logger::try_init();
@@ -777,3 +700,86 @@ fn clone_userdata() {
 
     assert_eq!(*result, Test(123));
 }
+
+#[cfg(feature = "serialization")]
+mod serialization {
+    use super::*;
+
+    use gluon::vm::api::de::De;
+
+    #[test]
+    fn use_type_from_type_field() {
+        let _ = ::env_logger::try_init();
+        let vm = make_vm();
+
+        #[derive(Serialize, Deserialize, PartialEq, Debug)]
+        enum Test {
+            A(i32),
+            B(String),
+            C,
+        }
+        impl VmType for Test {
+            type Type = Self;
+            fn make_type(vm: &Thread) -> ArcType {
+                if let Some(typ) = vm.get_type::<Self>() {
+                    return typ;
+                }
+
+                let (name, typ) = gluon::vm::api::typ::from_rust::<Self>(vm).unwrap();
+                vm.register_type_as(
+                    name.clone(),
+                    Alias::new(name, Vec::new(), typ),
+                    ::std::any::TypeId::of::<Self>(),
+                )
+                .unwrap()
+            }
+        }
+
+        add_extern_module(&vm, "test_types", |vm| {
+            ExternModule::new(vm, record! { type Test => Test })
+        });
+        let text = r#"
+        let { Test } = import! test_types
+        B "abc"
+    "#;
+        let (De(actual), _) = vm
+            .run_expr::<De<Test>>("test", text)
+            .unwrap_or_else(|err| panic!("{}", err));
+        assert_eq!(actual, Test::B("abc".to_string()));
+    }
+
+    #[test]
+    fn return_btreemap() {
+        let _ = ::env_logger::try_init();
+
+        let vm = make_vm();
+
+        add_extern_module_with_deps(
+            &vm,
+            "test",
+            |vm| {
+                ExternModule::new(
+                    vm,
+                    primitive!(1, "test", |()| {
+                        vec![("a".to_string(), 1), ("b".to_string(), 2)]
+                            .into_iter()
+                            .collect::<BTreeMap<_, _>>()
+                    }),
+                )
+            },
+            vec!["std.map".into(), "std.json.de".into()],
+        );
+
+        vm.run_expr::<()>("", "let _ = import! test in ()")
+            .unwrap_or_else(|err| panic!("{}", err));
+        let (result, _) = vm
+            .run_expr::<BTreeMap<_, _>>("", "(import! test) ()")
+            .unwrap_or_else(|err| panic!("{}", err));
+        assert_eq!(
+            result,
+            vec![("a".to_string(), 1), ("b".to_string(), 2)]
+                .into_iter()
+                .collect::<BTreeMap<_, _>>()
+        );
+    }
+}