Fix compile.links for vala

Fixes issue #12959
compiler.links command for vala crashes
diff --git a/mesonbuild/compilers/vala.py b/mesonbuild/compilers/vala.py
index 52097c9..839d544 100644
--- a/mesonbuild/compilers/vala.py
+++ b/mesonbuild/compilers/vala.py
@@ -7,13 +7,17 @@
 import typing as T
 
 from .. import mlog
+from .. import mesonlib
 from ..mesonlib import EnvironmentException, version_compare, LibType, OptionKey
 from .compilers import CompileCheckMode, Compiler
+from ..arglist import CompilerArgs
 
 if T.TYPE_CHECKING:
+    from ..coredata import KeyedOptionDictType
     from ..envconfig import MachineInfo
     from ..environment import Environment
     from ..mesonlib import MachineChoice
+    from ..dependencies import Dependency
 
 class ValaCompiler(Compiler):
 
@@ -25,6 +29,7 @@
         super().__init__([], exelist, version, for_machine, info, is_cross=is_cross)
         self.version = version
         self.base_options = {OptionKey('b_colorout')}
+        self.force_link = False
 
     def needs_static_linker(self) -> bool:
         return False # Because compiles into C.
@@ -41,6 +46,20 @@
     def get_compile_only_args(self) -> T.List[str]:
         return [] # Because compiles into C.
 
+    def get_compiler_args_for_mode(self, mode: CompileCheckMode) -> T.List[str]:
+        args: T.List[str] = []
+        if mode is CompileCheckMode.LINK and self.force_link:
+            return args
+        args += self.get_always_args()
+        if mode is CompileCheckMode.COMPILE:
+            args += self.get_compile_only_args()
+        elif mode is CompileCheckMode.PREPROCESS:
+            args += self.get_preprocess_only_args()
+        return args
+
+    def get_preprocess_only_args(self) -> T.List[str]:
+        return []
+
     def get_pic_args(self) -> T.List[str]:
         return []
 
@@ -119,3 +138,24 @@
 
     def thread_link_flags(self, env: 'Environment') -> T.List[str]:
         return []
+
+    def get_option_link_args(self, options: 'KeyedOptionDictType') -> T.List[str]:
+        return []
+
+    def links(self, code: 'mesonlib.FileOrString', env: 'Environment', *,
+              compiler: T.Optional['Compiler'] = None,
+              extra_args: T.Union[None, T.List[str], CompilerArgs, T.Callable[[CompileCheckMode], T.List[str]]] = None,
+              dependencies: T.Optional[T.List['Dependency']] = None,
+              disable_cache: bool = False) -> T.Tuple[bool, bool]:
+        self.force_link = True
+        if compiler:
+            with compiler._build_wrapper(code, env, dependencies=dependencies, want_output=True) as r:
+                objfile = mesonlib.File.from_absolute_file(r.output_name)
+                result = self.compiles(objfile, env, extra_args=extra_args,
+                                       dependencies=dependencies, mode=CompileCheckMode.LINK, disable_cache=True)
+                self.force_link = False
+                return result
+        result = self.compiles(code, env, extra_args=extra_args,
+                               dependencies=dependencies, mode=CompileCheckMode.LINK, disable_cache=disable_cache)
+        self.force_link = False
+        return result
diff --git a/test cases/vala/29 compiler.links/meson.build b/test cases/vala/29 compiler.links/meson.build
new file mode 100644
index 0000000..f21d595
--- /dev/null
+++ b/test cases/vala/29 compiler.links/meson.build
@@ -0,0 +1,24 @@
+project('link-test', ['c', 'vala'], version: '0.1')
+
+valac = meson.get_compiler('vala')
+
+code = '''void main() {
+  const double PI3 = 1.047197551196597746154214461093167628;
+  var a = GLib.Math.cos (PI3);
+  stdout.printf ("%f\n", a); }'''
+
+# test 1; code should link
+code_links = valac.links(
+  code,
+  args: '--Xcc=-lm',
+  name: 'links with math library? == YES',
+)
+assert (code_links, 'Math library should link successfully.')
+
+# test 2; code should not link
+code_links = valac.links(
+  code,
+  args: '--Xcc=-lfake_library_90DFE450330A',
+  name: 'links with fake library? == NO',
+)
+assert (not code_links, 'Fake library should not link successfully.')