]> git.lizzy.rs Git - rust.git/commitdiff
skip the uninhabitated check and comments
authorGus Wynn <guswynn@gmail.com>
Sat, 11 Sep 2021 19:06:05 +0000 (12:06 -0700)
committerGus Wynn <guswynn@gmail.com>
Sat, 11 Sep 2021 19:10:06 +0000 (12:10 -0700)
12 files changed:
compiler/rustc_lint_defs/src/builtin.rs
compiler/rustc_passes/src/check_attr.rs
compiler/rustc_typeck/src/check/generator_interior.rs
src/test/ui/lint/must_not_suspend/boxed.rs
src/test/ui/lint/must_not_suspend/feature-gate-must_not_suspend.rs
src/test/ui/lint/must_not_suspend/other_items.rs [new file with mode: 0644]
src/test/ui/lint/must_not_suspend/other_items.stderr [new file with mode: 0644]
src/test/ui/lint/must_not_suspend/return.rs
src/test/ui/lint/must_not_suspend/return.stderr
src/test/ui/lint/must_not_suspend/trait.rs
src/test/ui/lint/must_not_suspend/unit.rs
src/test/ui/lint/must_not_suspend/warn.rs

index 386435034b6aca77e235578dc6e60d9bd004d19f..0463cb2b7655310be2d7ad0c999c6cff50901cfb 100644 (file)
 }
 
 declare_lint! {
-    /// [the reference]: https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-must_use-attribute
+    /// The `must_not_suspend` lint detects values that are marked with the `#[must_not_suspend]`
+    /// attribute being held across yield points. A "yield" point is usually a `.await` in an async
+    /// function.
+    ///
+    /// This attribute can be used to mark values that are semantically incorrect across yields
+    /// (like certain types of timers), values that have async alternatives, and values that
+    /// regularly cause problems with the `Send`-ness of async fn's returned futures (like
+    /// `MutexGuard`'s)
+    ///
+    /// ### Example
+    ///
+    /// ```rust
+    /// #[must_not_suspend]
+    /// struct SyncThing {}
+    ///
+    /// async fn yield() {}
+    ///
+    /// pub async fn uhoh() {
+    ///     let guard = SyncThing {};
+    ///     yield().await;
+    /// }
+    /// ```
     pub MUST_NOT_SUSPEND,
     Warn,
     "Use of a `#[must_not_suspend]` value across a yield point",
index a31f3fe281ee9918039793addf2af3ca75f17125..3e59fc4f551594ec22b7725e5efc9b6c628b8ae1 100644 (file)
@@ -1018,15 +1018,15 @@ fn check_doc_attrs(
     /// Checks if `#[must_not_suspend]` is applied to a function. Returns `true` if valid.
     fn check_must_not_suspend(&self, attr: &Attribute, span: &Span, target: Target) -> bool {
         match target {
-            Target::Fn | Target::Method(..) | Target::ForeignFn | Target::Closure => {
+            Target::Struct | Target::Enum | Target::Union | Target::Trait => true,
+            _ => {
                 self.tcx
                     .sess
-                    .struct_span_err(attr.span, "`must_not_suspend` attribute should be applied to a struct, enum, `impl Trait`, or `dyn Trait`")
-                        .span_label(*span, "is a function")
+                    .struct_span_err(attr.span, "`must_not_suspend` attribute should be applied to a struct, enum, or trait")
+                        .span_label(*span, "is not a struct, enum, or trait")
                         .emit();
                 false
             }
-            _ => true,
         }
     }
 
index 118c6e9a27363da4243c6f3ad2c22d7dc02c49e5..c4c09a55a6e0060bba267dc7766ef66b80935bd0 100644 (file)
@@ -124,7 +124,6 @@ fn record(
 
                 check_must_not_suspend_ty(
                     self.fcx,
-                    ty::ParamEnv::empty(),
                     ty,
                     hir_id,
                     expr,
@@ -454,7 +453,6 @@ fn visit_pat(&mut self, pat: &'tcx Pat<'tcx>) {
 // for creating must_use diagnostics
 pub fn check_must_not_suspend_ty<'tcx>(
     fcx: &FnCtxt<'_, 'tcx>,
-    param_env: ty::ParamEnv<'tcx>,
     ty: Ty<'tcx>,
     hir_id: HirId,
     expr: Option<&'tcx Expr<'tcx>>,
@@ -464,8 +462,10 @@ pub fn check_must_not_suspend_ty<'tcx>(
     descr_post: &str,
     plural_len: usize,
 ) -> bool {
+    debug!("FOUND TYPE: {:?}", ty);
     if ty.is_unit()
-        || fcx.tcx.is_ty_uninhabited_from(fcx.tcx.parent_module(hir_id).to_def_id(), ty, param_env)
+    // || fcx.tcx.is_ty_uninhabited_from(fcx.tcx.parent_module(hir_id).to_def_id(), ty, fcx.param_env)
+    // FIXME: should this check is_ty_uninhabited_from
     {
         return true;
     }
@@ -478,7 +478,6 @@ pub fn check_must_not_suspend_ty<'tcx>(
             let descr_pre = &format!("{}boxed ", descr_pre);
             check_must_not_suspend_ty(
                 fcx,
-                param_env,
                 boxed_ty,
                 hir_id,
                 expr,
@@ -547,28 +546,24 @@ pub fn check_must_not_suspend_ty<'tcx>(
         }
         ty::Tuple(ref tys) => {
             let mut has_emitted = false;
-            /*
-            let spans = if let hir::ExprKind::Tup(comps) = &expr.kind {
+            let spans = if let Some(hir::ExprKind::Tup(comps)) = expr.map(|e| &e.kind) {
                 debug_assert_eq!(comps.len(), tys.len());
                 comps.iter().map(|e| e.span).collect()
             } else {
                 vec![]
             };
-            */
-            let spans = vec![];
             for (i, ty) in tys.iter().map(|k| k.expect_ty()).enumerate() {
                 let descr_post = &format!(" in tuple element {}", i);
                 let span = *spans.get(i).unwrap_or(&source_span);
                 if check_must_not_suspend_ty(
-                    fcx, param_env, ty, hir_id, expr, span, yield_span, descr_pre, descr_post,
-                    plural_len,
+                    fcx, ty, hir_id, expr, span, yield_span, descr_pre, descr_post, plural_len,
                 ) {
                     has_emitted = true;
                 }
             }
             has_emitted
         }
-        ty::Array(ty, len) => match len.try_eval_usize(fcx.tcx, param_env) {
+        ty::Array(ty, len) => match len.try_eval_usize(fcx.tcx, fcx.param_env) {
             // If the array is empty we don't lint, to avoid false positives
             Some(0) | None => false,
             // If the array is definitely non-empty, we can do `#[must_use]` checking.
@@ -576,7 +571,6 @@ pub fn check_must_not_suspend_ty<'tcx>(
                 let descr_pre = &format!("{}array{} of ", descr_pre, plural_suffix,);
                 check_must_not_suspend_ty(
                     fcx,
-                    param_env,
                     ty,
                     hir_id,
                     expr,
index d64d07e5e0df652f8081753a978b8eb10ebe122a..1f823fc559d40b4fd99f2c854805391f296b00f9 100644 (file)
@@ -17,7 +17,7 @@ fn bar() -> Box<Umm> {
 async fn other() {}
 
 pub async fn uhoh() {
-    let _guard = bar(); //~ boxed `Umm` held across
+    let _guard = bar(); //~ ERROR boxed `Umm` held across
     other().await;
 }
 
index aff8ff33b65a8924941f6c5f6f292adce184168a..1554408c174ce92350685bab1f0c54ccfaa4c6fd 100644 (file)
@@ -1,6 +1,6 @@
 // edition:2018
 
-#[must_not_suspend = "You gotta use Umm's, ya know?"] //~ the `#[must_not_suspend]`
+#[must_not_suspend = "You gotta use Umm's, ya know?"] //~ ERROR the `#[must_not_suspend]`
 struct Umm {
     _i: i64
 }
diff --git a/src/test/ui/lint/must_not_suspend/other_items.rs b/src/test/ui/lint/must_not_suspend/other_items.rs
new file mode 100644 (file)
index 0000000..5aa1abb
--- /dev/null
@@ -0,0 +1,8 @@
+// edition:2018
+#![feature(must_not_suspend)]
+#![deny(must_not_suspend)]
+
+#[must_not_suspend] //~ ERROR attribute should be
+mod inner {}
+
+fn main() {}
diff --git a/src/test/ui/lint/must_not_suspend/other_items.stderr b/src/test/ui/lint/must_not_suspend/other_items.stderr
new file mode 100644 (file)
index 0000000..41c8896
--- /dev/null
@@ -0,0 +1,10 @@
+error: `must_not_suspend` attribute should be applied to a struct, enum, or trait
+  --> $DIR/other_items.rs:5:1
+   |
+LL | #[must_not_suspend]
+   | ^^^^^^^^^^^^^^^^^^^
+LL | mod inner {}
+   | ------------ is not a struct, enum, or trait
+
+error: aborting due to previous error
+
index 5f80e78937628723a9e66719be5a97cf222071ec..5b1fa5e272118470b48831153aa6ce091b1a37e0 100644 (file)
@@ -2,7 +2,7 @@
 #![feature(must_not_suspend)]
 #![deny(must_not_suspend)]
 
-#[must_not_suspend] //~ attribute should be
+#[must_not_suspend] //~ ERROR attribute should be
 fn foo() -> i32 {
     0
 }
index ff1798320cf8e03832c4fc95ed23902cec96f528..fdada85eb4d1caf761044bc01ee7872a4cc07034 100644 (file)
@@ -1,4 +1,4 @@
-error: `must_not_suspend` attribute should be applied to a struct, enum, `impl Trait`, or `dyn Trait`
+error: `must_not_suspend` attribute should be applied to a struct, enum, or trait
   --> $DIR/return.rs:5:1
    |
 LL |   #[must_not_suspend]
@@ -6,7 +6,7 @@ LL |   #[must_not_suspend]
 LL | / fn foo() -> i32 {
 LL | |     0
 LL | | }
-   | |_- is a function
+   | |_- is not a struct, enum, or trait
 
 error: aborting due to previous error
 
index 0438e072ce67e09f25c3a624185c2dd888bb3e9e..6c911cb4b0f09d03cd7cfef2d38558519affb7fd 100644 (file)
@@ -18,8 +18,8 @@ fn r#dyn() -> Box<dyn Wow> {
 async fn other() {}
 
 pub async fn uhoh() {
-    let _guard1 = r#impl(); //~ implementer of `Wow` held across
-    let _guard2 = r#dyn(); //~ boxed `Wow` trait object held across
+    let _guard1 = r#impl(); //~ ERROR implementer of `Wow` held across
+    let _guard2 = r#dyn(); //~ ERROR boxed `Wow` trait object held across
 
     other().await;
 }
index 4e87b801114e90aef3113e3d1211681e0e3e6200..d3a19f704324ddefa3d910a5d788d5bbb53233fa 100644 (file)
@@ -17,7 +17,7 @@ fn bar() -> Umm {
 async fn other() {}
 
 pub async fn uhoh() {
-    let _guard = bar(); //~ `Umm` held across
+    let _guard = bar(); //~ ERROR `Umm` held across
     other().await;
 }
 
index d0d723848075981a31f0e90484065080e84dba9c..50a696ba52322adb11dab5942328ae665f85fb2d 100644 (file)
@@ -17,7 +17,7 @@ fn bar() -> Umm {
 async fn other() {}
 
 pub async fn uhoh() {
-    let _guard = bar(); //~ `Umm` held across
+    let _guard = bar(); //~ WARNING `Umm` held across
     other().await;
 }