]> git.lizzy.rs Git - rust.git/commitdiff
Rollup merge of #96641 - oli-obk:bool_args, r=wesleywiser
authorYuki Okushi <jtitor@2k36.org>
Tue, 3 May 2022 05:59:01 +0000 (14:59 +0900)
committerGitHub <noreply@github.com>
Tue, 3 May 2022 05:59:01 +0000 (14:59 +0900)
Use a yes/no enum instead of a bool.

The bool's meaning wasn't obvious to me at some call sites.

1  2 
compiler/rustc_resolve/src/late.rs

index b89b9c376af63efcbca40176c6fb8b06e3623734,8b065fb036b0f0c36a2c7a2c6d186fcce7ffd664..ab353128cbcced62dc7b0071de6a11845b58b3ac
@@@ -94,6 -94,12 +94,12 @@@ crate enum HasGenericParams 
      No,
  }
  
+ impl HasGenericParams {
+     fn force_yes_if(self, b: bool) -> Self {
+         if b { Self::Yes } else { self }
+     }
+ }
  #[derive(Copy, Clone, Debug, Eq, PartialEq)]
  crate enum ConstantItemKind {
      Const,
@@@ -125,9 -131,9 +131,9 @@@ crate enum RibKind<'a> 
  
      /// We're in a constant item. Can't refer to dynamic stuff.
      ///
-     /// The `bool` indicates if this constant may reference generic parameters
-     /// and is used to only allow generic parameters to be used in trivial constant expressions.
-     ConstantItemRibKind(bool, Option<(Ident, ConstantItemKind)>),
+     /// The item may reference generic parameters in trivial constant expressions.
+     /// All other constants aren't allowed to use generic params at all.
+     ConstantItemRibKind(HasGenericParams, Option<(Ident, ConstantItemKind)>),
  
      /// We passed through a module.
      ModuleRibKind(Module<'a>),
@@@ -826,19 -832,24 +832,24 @@@ impl<'a: 'ast, 'ast> Visitor<'ast> for 
                              // Note that we might not be inside of an repeat expression here,
                              // but considering that `IsRepeatExpr` is only relevant for
                              // non-trivial constants this is doesn't matter.
-                             self.with_constant_rib(IsRepeatExpr::No, true, None, |this| {
-                                 this.smart_resolve_path(
-                                     ty.id,
-                                     qself.as_ref(),
-                                     path,
-                                     PathSource::Expr(None),
-                                 );
-                                 if let Some(ref qself) = *qself {
-                                     this.visit_ty(&qself.ty);
-                                 }
-                                 this.visit_path(path, ty.id);
-                             });
+                             self.with_constant_rib(
+                                 IsRepeatExpr::No,
+                                 HasGenericParams::Yes,
+                                 None,
+                                 |this| {
+                                     this.smart_resolve_path(
+                                         ty.id,
+                                         qself.as_ref(),
+                                         path,
+                                         PathSource::Expr(None),
+                                     );
+                                     if let Some(ref qself) = *qself {
+                                         this.visit_ty(&qself.ty);
+                                     }
+                                     this.visit_path(path, ty.id);
+                                 },
+                             );
  
                              self.diagnostic_metadata.currently_processing_generics = prev;
                              return;
@@@ -1313,8 -1324,12 +1324,8 @@@ impl<'a: 'ast, 'b, 'ast> LateResolution
              // Figure out if this is a type/trait segment,
              // which may need lifetime elision performed.
              let type_def_id = match partial_res.base_res() {
 -                Res::Def(DefKind::AssocTy, def_id) if i + 2 == proj_start => {
 -                    self.r.parent(def_id).unwrap()
 -                }
 -                Res::Def(DefKind::Variant, def_id) if i + 1 == proj_start => {
 -                    self.r.parent(def_id).unwrap()
 -                }
 +                Res::Def(DefKind::AssocTy, def_id) if i + 2 == proj_start => self.r.parent(def_id),
 +                Res::Def(DefKind::Variant, def_id) if i + 1 == proj_start => self.r.parent(def_id),
                  Res::Def(DefKind::Struct, def_id)
                  | Res::Def(DefKind::Union, def_id)
                  | Res::Def(DefKind::Enum, def_id)
                                                      // not used as part of the type system, this is far less surprising.
                                                      this.with_constant_rib(
                                                          IsRepeatExpr::No,
-                                                         true,
+                                                         HasGenericParams::Yes,
                                                          None,
                                                          |this| this.visit_expr(expr),
                                                      );
                          // so it doesn't matter whether this is a trivial constant.
                          this.with_constant_rib(
                              IsRepeatExpr::No,
-                             true,
+                             HasGenericParams::Yes,
                              Some((item.ident, constant_item_kind)),
                              |this| this.visit_expr(expr),
                          );
      // Note that we intentionally still forbid `[0; N + 1]` during
      // name resolution so that we don't extend the future
      // compat lint to new cases.
+     #[instrument(level = "debug", skip(self, f))]
      fn with_constant_rib(
          &mut self,
          is_repeat: IsRepeatExpr,
-         is_trivial: bool,
+         may_use_generics: HasGenericParams,
          item: Option<(Ident, ConstantItemKind)>,
          f: impl FnOnce(&mut Self),
      ) {
-         debug!("with_constant_rib: is_repeat={:?} is_trivial={}", is_repeat, is_trivial);
-         self.with_rib(ValueNS, ConstantItemRibKind(is_trivial, item), |this| {
+         self.with_rib(ValueNS, ConstantItemRibKind(may_use_generics, item), |this| {
              this.with_rib(
                  TypeNS,
-                 ConstantItemRibKind(is_repeat == IsRepeatExpr::Yes || is_trivial, item),
+                 ConstantItemRibKind(
+                     may_use_generics.force_yes_if(is_repeat == IsRepeatExpr::Yes),
+                     item,
+                 ),
                  |this| {
-                     this.with_label_rib(ConstantItemRibKind(is_trivial, item), f);
+                     this.with_label_rib(ConstantItemRibKind(may_use_generics, item), f);
                  },
              )
          });
                                                          // not used as part of the type system, this is far less surprising.
                                                          this.with_constant_rib(
                                                              IsRepeatExpr::No,
-                                                             true,
+                                                             HasGenericParams::Yes,
                                                              None,
                                                              |this| {
                                                                  visit::walk_assoc_item(
          debug!("resolve_anon_const {:?} is_repeat: {:?}", constant, is_repeat);
          self.with_constant_rib(
              is_repeat,
-             constant.value.is_potential_trivial_const_param(),
+             if constant.value.is_potential_trivial_const_param() {
+                 HasGenericParams::Yes
+             } else {
+                 HasGenericParams::No
+             },
              None,
              |this| visit::walk_anon_const(this, constant),
          );
                      if const_args.contains(&idx) {
                          self.with_constant_rib(
                              IsRepeatExpr::No,
-                             argument.is_potential_trivial_const_param(),
+                             if argument.is_potential_trivial_const_param() {
+                                 HasGenericParams::Yes
+                             } else {
+                                 HasGenericParams::No
+                             },
                              None,
                              |this| {
                                  this.resolve_expr(argument, None);