]> git.lizzy.rs Git - rust.git/commitdiff
Add dummy implementations of env! and option_env! builtins
authorFlorian Diebold <flodiebold@gmail.com>
Sat, 21 Dec 2019 12:33:44 +0000 (13:33 +0100)
committerFlorian Diebold <flodiebold@gmail.com>
Sat, 21 Dec 2019 12:34:43 +0000 (13:34 +0100)
They don't do anything except return the correct type.

Also refactor the builtin macro tests a bit.

crates/ra_hir_expand/src/builtin_macro.rs
crates/ra_hir_expand/src/name.rs
crates/ra_hir_expand/src/quote.rs

index 857f8a4441ea4884f0ff821a0f0bfb39d1df359c..2c119269c13de2d57b932871126a76ac7c263859 100644 (file)
@@ -26,6 +26,13 @@ pub fn expand(
                 };
                 expander(db, id, tt)
             }
+
+            fn by_name(ident: &name::Name) -> Option<BuiltinFnLikeExpander> {
+                match ident {
+                    $( id if id == &name::name![$name] => Some(BuiltinFnLikeExpander::$kind), )*
+                    _ => return None,
+                }
+            }
         }
 
         pub fn find_builtin_macro(
@@ -33,10 +40,7 @@ pub fn find_builtin_macro(
             krate: CrateId,
             ast_id: AstId<ast::MacroCall>,
         ) -> Option<MacroDefId> {
-            let kind = match ident {
-                 $( id if id == &name::name![$name] => BuiltinFnLikeExpander::$kind, )*
-                 _ => return None,
-            };
+            let kind = BuiltinFnLikeExpander::by_name(ident)?;
 
             Some(MacroDefId { krate: Some(krate), ast_id: Some(ast_id), kind: MacroDefKind::BuiltIn(kind) })
         }
@@ -50,6 +54,8 @@ pub fn find_builtin_macro(
     (line, Line) => line_expand,
     (stringify, Stringify) => stringify_expand,
     (format_args, FormatArgs) => format_args_expand,
+    (env, Env) => env_expand,
+    (option_env, OptionEnv) => option_env_expand,
     // format_args_nl only differs in that it adds a newline in the end,
     // so we use the same stub expansion for now
     (format_args_nl, FormatArgsNl) => format_args_expand
@@ -121,6 +127,28 @@ fn stringify_expand(
     Ok(expanded)
 }
 
+fn env_expand(
+    _db: &dyn AstDatabase,
+    _id: MacroCallId,
+    _tt: &tt::Subtree,
+) -> Result<tt::Subtree, mbe::ExpandError> {
+    // dummy implementation for type-checking purposes
+    let expanded = quote! { "" };
+
+    Ok(expanded)
+}
+
+fn option_env_expand(
+    _db: &dyn AstDatabase,
+    _id: MacroCallId,
+    _tt: &tt::Subtree,
+) -> Result<tt::Subtree, mbe::ExpandError> {
+    // dummy implementation for type-checking purposes
+    let expanded = quote! { std::option::Option::None::<&str> };
+
+    Ok(expanded)
+}
+
 fn to_col_number(db: &dyn AstDatabase, file: HirFileId, pos: TextUnit) -> usize {
     // FIXME: Use expansion info
     let file_id = file.original_file(db);
@@ -248,10 +276,11 @@ fn format_args_expand(
 #[cfg(test)]
 mod tests {
     use super::*;
-    use crate::{test_db::TestDB, MacroCallKind, MacroCallLoc};
+    use crate::{name::AsName, test_db::TestDB, MacroCallKind, MacroCallLoc};
     use ra_db::{fixture::WithFixture, SourceDatabase};
+    use ra_syntax::ast::NameOwner;
 
-    fn expand_builtin_macro(s: &str, expander: BuiltinFnLikeExpander) -> String {
+    fn expand_builtin_macro(s: &str) -> String {
         let (db, file_id) = TestDB::with_single_file(&s);
         let parsed = db.parse(file_id);
         let macro_calls: Vec<_> =
@@ -259,6 +288,9 @@ fn expand_builtin_macro(s: &str, expander: BuiltinFnLikeExpander) -> String {
 
         let ast_id_map = db.ast_id_map(file_id.into());
 
+        let expander =
+            BuiltinFnLikeExpander::by_name(&macro_calls[0].name().unwrap().as_name()).unwrap();
+
         // the first one should be a macro_rules
         let def = MacroDefId {
             krate: Some(CrateId(0)),
@@ -284,25 +316,23 @@ fn expand_builtin_macro(s: &str, expander: BuiltinFnLikeExpander) -> String {
     fn test_column_expand() {
         let expanded = expand_builtin_macro(
             r#"
-        #[rustc_builtin_macro]
-        macro_rules! column {() => {}}
-        column!()
-"#,
-            BuiltinFnLikeExpander::Column,
+            #[rustc_builtin_macro]
+            macro_rules! column {() => {}}
+            column!()
+            "#,
         );
 
-        assert_eq!(expanded, "9");
+        assert_eq!(expanded, "13");
     }
 
     #[test]
     fn test_line_expand() {
         let expanded = expand_builtin_macro(
             r#"
-        #[rustc_builtin_macro]
-        macro_rules! line {() => {}}
-        line!()
-"#,
-            BuiltinFnLikeExpander::Line,
+            #[rustc_builtin_macro]
+            macro_rules! line {() => {}}
+            line!()
+            "#,
         );
 
         assert_eq!(expanded, "4");
@@ -312,25 +342,49 @@ macro_rules! line {() => {}}
     fn test_stringify_expand() {
         let expanded = expand_builtin_macro(
             r#"
-        #[rustc_builtin_macro]
-        macro_rules! stringify {() => {}}
-        stringify!(a b c)
-"#,
-            BuiltinFnLikeExpander::Stringify,
+            #[rustc_builtin_macro]
+            macro_rules! stringify {() => {}}
+            stringify!(a b c)
+            "#,
         );
 
         assert_eq!(expanded, "\"a b c\"");
     }
 
+    #[test]
+    fn test_env_expand() {
+        let expanded = expand_builtin_macro(
+            r#"
+            #[rustc_builtin_macro]
+            macro_rules! env {() => {}}
+            env!("TEST_ENV_VAR")
+            "#,
+        );
+
+        assert_eq!(expanded, "\"\"");
+    }
+
+    #[test]
+    fn test_option_env_expand() {
+        let expanded = expand_builtin_macro(
+            r#"
+            #[rustc_builtin_macro]
+            macro_rules! option_env {() => {}}
+            option_env!("TEST_ENV_VAR")
+            "#,
+        );
+
+        assert_eq!(expanded, "std::option::Option::None:: <&str>");
+    }
+
     #[test]
     fn test_file_expand() {
         let expanded = expand_builtin_macro(
             r#"
-        #[rustc_builtin_macro]
-        macro_rules! file {() => {}}
-        file!()
-"#,
-            BuiltinFnLikeExpander::File,
+            #[rustc_builtin_macro]
+            macro_rules! file {() => {}}
+            file!()
+            "#,
         );
 
         assert_eq!(expanded, "\"\"");
@@ -340,14 +394,13 @@ macro_rules! file {() => {}}
     fn test_compile_error_expand() {
         let expanded = expand_builtin_macro(
             r#"
-        #[rustc_builtin_macro]
-        macro_rules! compile_error {
-            ($msg:expr) => ({ /* compiler built-in */ });
-            ($msg:expr,) => ({ /* compiler built-in */ })
-        }
-        compile_error!("error!");
-"#,
-            BuiltinFnLikeExpander::CompileError,
+            #[rustc_builtin_macro]
+            macro_rules! compile_error {
+                ($msg:expr) => ({ /* compiler built-in */ });
+                ($msg:expr,) => ({ /* compiler built-in */ })
+            }
+            compile_error!("error!");
+            "#,
         );
 
         assert_eq!(expanded, r#"loop{"error!"}"#);
@@ -357,14 +410,13 @@ macro_rules! compile_error {
     fn test_format_args_expand() {
         let expanded = expand_builtin_macro(
             r#"
-        #[rustc_builtin_macro]
-        macro_rules! format_args {
-            ($fmt:expr) => ({ /* compiler built-in */ });
-            ($fmt:expr, $($args:tt)*) => ({ /* compiler built-in */ })
-        }
-        format_args!("{} {:?}", arg1(a, b, c), arg2);
-"#,
-            BuiltinFnLikeExpander::FormatArgs,
+            #[rustc_builtin_macro]
+            macro_rules! format_args {
+                ($fmt:expr) => ({ /* compiler built-in */ });
+                ($fmt:expr, $($args:tt)*) => ({ /* compiler built-in */ })
+            }
+            format_args!("{} {:?}", arg1(a, b, c), arg2);
+            "#,
         );
 
         assert_eq!(
index fd02ffa4e12d1ce197771545d31506c354bb6cd6..e62693b6852397b12cc07a34e078251fe5c2cc16 100644 (file)
@@ -170,6 +170,8 @@ macro_rules! known_names {
         stringify,
         format_args,
         format_args_nl,
+        env,
+        option_env,
         // Builtin derives
         Copy,
         Clone,
index 49155fe6262c234d8bd222a4e13ac75d2dc41b80..4de219ce463e39df243f4f5fd5eabc9c99fd3a30 100644 (file)
@@ -102,6 +102,8 @@ macro_rules! __quote {
     ( : ) => {$crate::__quote!(@PUNCT ':')};
     ( :: ) => {$crate::__quote!(@PUNCT ':', ':')};
     ( . ) => {$crate::__quote!(@PUNCT '.')};
+    ( < ) => {$crate::__quote!(@PUNCT '<')};
+    ( > ) => {$crate::__quote!(@PUNCT '>')};
 
     ( $first:tt $($tail:tt)+ ) => {
         {