LCOV - code coverage report
Current view: top level - gcc/rust/checks/errors - rust-unsafe-checker.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 72.2 % 467 337
Test Date: 2025-06-21 16:26:05 Functions: 55.9 % 136 76
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : // Copyright (C) 2020-2025 Free Software Foundation, Inc.
       2                 :             : 
       3                 :             : // This file is part of GCC.
       4                 :             : 
       5                 :             : // GCC is free software; you can redistribute it and/or modify it under
       6                 :             : // the terms of the GNU General Public License as published by the Free
       7                 :             : // Software Foundation; either version 3, or (at your option) any later
       8                 :             : // version.
       9                 :             : 
      10                 :             : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      11                 :             : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
      12                 :             : // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      13                 :             : // for more details.
      14                 :             : 
      15                 :             : // You should have received a copy of the GNU General Public License
      16                 :             : // along with GCC; see the file COPYING3.  If not see
      17                 :             : // <http://www.gnu.org/licenses/>.
      18                 :             : 
      19                 :             : #include "rust-unsafe-checker.h"
      20                 :             : #include "rust-hir.h"
      21                 :             : #include "rust-hir-expr.h"
      22                 :             : #include "rust-hir-stmt.h"
      23                 :             : #include "rust-hir-item.h"
      24                 :             : #include "rust-attribute-values.h"
      25                 :             : #include "rust-system.h"
      26                 :             : #include "rust-immutable-name-resolution-context.h"
      27                 :             : 
      28                 :             : // for flag_name_resolution_2_0
      29                 :             : #include "options.h"
      30                 :             : 
      31                 :             : namespace Rust {
      32                 :             : namespace HIR {
      33                 :             : 
      34                 :        4313 : UnsafeChecker::UnsafeChecker ()
      35                 :        4313 :   : context (*Resolver::TypeCheckContext::get ()),
      36                 :        4313 :     resolver (*Resolver::Resolver::get ()),
      37                 :        8626 :     mappings (Analysis::Mappings::get ())
      38                 :        4313 : {}
      39                 :             : 
      40                 :             : void
      41                 :        4313 : UnsafeChecker::go (HIR::Crate &crate)
      42                 :             : {
      43                 :       20274 :   for (auto &item : crate.get_items ())
      44                 :       15961 :     item->accept_vis (*this);
      45                 :        4313 : }
      46                 :             : 
      47                 :             : static void
      48                 :         843 : check_static_mut (HIR::Item *maybe_static, location_t locus)
      49                 :             : {
      50                 :         843 :   if (maybe_static->get_hir_kind () == Node::BaseKind::VIS_ITEM)
      51                 :             :     {
      52                 :         843 :       auto item = static_cast<Item *> (maybe_static);
      53                 :         843 :       if (item->get_item_kind () == Item::ItemKind::Static)
      54                 :             :         {
      55                 :          32 :           auto static_item = static_cast<StaticItem *> (item);
      56                 :          32 :           if (static_item->is_mut ())
      57                 :           8 :             rust_error_at (
      58                 :             :               locus, "use of mutable static requires unsafe function or block");
      59                 :             :         }
      60                 :             :     }
      61                 :         843 : }
      62                 :             : 
      63                 :             : static void
      64                 :           2 : check_extern_static (HIR::ExternalItem *maybe_static, location_t locus)
      65                 :             : {
      66                 :           2 :   if (maybe_static->get_extern_kind () == ExternalItem::ExternKind::Static)
      67                 :           2 :     rust_error_at (locus,
      68                 :             :                    "use of extern static requires unsafe function or block");
      69                 :           2 : }
      70                 :             : 
      71                 :             : void
      72                 :       22547 : UnsafeChecker::check_use_of_static (HirId node_id, location_t locus)
      73                 :             : {
      74                 :       22547 :   if (unsafe_context.is_in_context ())
      75                 :             :     return;
      76                 :             : 
      77                 :       13900 :   if (auto maybe_static_mut = mappings.lookup_hir_item (node_id))
      78                 :         843 :     check_static_mut (*maybe_static_mut, locus);
      79                 :             : 
      80                 :       13900 :   if (auto maybe_extern_static = mappings.lookup_hir_extern_item (node_id))
      81                 :           4 :     check_extern_static (static_cast<ExternalItem *> (
      82                 :           2 :                            maybe_extern_static->first),
      83                 :             :                          locus);
      84                 :             : }
      85                 :             : 
      86                 :             : static void
      87                 :        3059 : check_unsafe_call (HIR::Function *fn, location_t locus, const std::string &kind)
      88                 :             : {
      89                 :        3059 :   if (fn->get_qualifiers ().is_unsafe ())
      90                 :           6 :     rust_error_at (locus, ErrorCode::E0133,
      91                 :             :                    "call to unsafe %s requires unsafe function or block",
      92                 :             :                    kind.c_str ());
      93                 :        3059 : }
      94                 :             : 
      95                 :             : static bool
      96                 :          26 : is_safe_intrinsic (const std::string &fn_name)
      97                 :             : {
      98                 :          26 :   static const std::unordered_set<std::string> safe_intrinsics = {
      99                 :             :     "abort",
     100                 :             :     "size_of",
     101                 :             :     "min_align_of",
     102                 :             :     "needs_drop",
     103                 :             :     "caller_location",
     104                 :             :     "add_with_overflow",
     105                 :             :     "sub_with_overflow",
     106                 :             :     "mul_with_overflow",
     107                 :             :     "wrapping_add",
     108                 :             :     "wrapping_sub",
     109                 :             :     "wrapping_mul",
     110                 :             :     "saturating_add",
     111                 :             :     "saturating_sub",
     112                 :             :     "rotate_left",
     113                 :             :     "rotate_right",
     114                 :             :     "ctpop",
     115                 :             :     "ctlz",
     116                 :             :     "cttz",
     117                 :             :     "bswap",
     118                 :             :     "bitreverse",
     119                 :             :     "discriminant_value",
     120                 :             :     "type_id",
     121                 :             :     "likely",
     122                 :             :     "unlikely",
     123                 :             :     "ptr_guaranteed_eq",
     124                 :             :     "ptr_guaranteed_ne",
     125                 :             :     "minnumf32",
     126                 :             :     "minnumf64",
     127                 :             :     "maxnumf32",
     128                 :             :     "rustc_peek",
     129                 :             :     "maxnumf64",
     130                 :             :     "type_name",
     131                 :             :     "forget",
     132                 :             :     "black_box",
     133                 :             :     "variant_count",
     134                 :          26 :   };
     135                 :             : 
     136                 :          26 :   return safe_intrinsics.find (fn_name) != safe_intrinsics.end ();
     137                 :             : }
     138                 :             : 
     139                 :             : static void
     140                 :          36 : check_extern_call (HIR::ExternalItem *maybe_fn, HIR::ExternBlock *parent_block,
     141                 :             :                    location_t locus)
     142                 :             : {
     143                 :             :   // We have multiple operations to perform here
     144                 :             :   //     1. Is the item an actual function we're calling
     145                 :             :   //     2. Is the block it's defined in an FFI block or an `extern crate` block
     146                 :             :   //
     147                 :             :   // It is not unsafe to call into other crates, so items defined in an `extern
     148                 :             :   // crate` must be callable without being in an unsafe context. On the other
     149                 :             :   // hand, any function defined in a block with a specific ABI (even `extern
     150                 :             :   // "Rust"` blocks) is unsafe to call
     151                 :             : 
     152                 :          36 :   if (maybe_fn->get_extern_kind () != ExternalItem::ExternKind::Function)
     153                 :             :     return;
     154                 :             : 
     155                 :             :   // Some intrinsics are safe to call
     156                 :          72 :   if (parent_block->get_abi () == Rust::ABI::INTRINSIC
     157                 :          88 :       && is_safe_intrinsic (maybe_fn->get_item_name ().as_string ()))
     158                 :             :     return;
     159                 :             : 
     160                 :          10 :   rust_error_at (locus,
     161                 :             :                  "call to extern function requires unsafe function or block");
     162                 :             : }
     163                 :             : 
     164                 :             : void
     165                 :        8149 : UnsafeChecker::check_function_call (HirId node_id, location_t locus)
     166                 :             : {
     167                 :        8149 :   if (unsafe_context.is_in_context ())
     168                 :        4142 :     return;
     169                 :             : 
     170                 :        4007 :   auto maybe_fn = mappings.lookup_hir_item (node_id);
     171                 :             : 
     172                 :        4007 :   if (maybe_fn
     173                 :        4007 :       && maybe_fn.value ()->get_item_kind () == Item::ItemKind::Function)
     174                 :        2443 :     check_unsafe_call (static_cast<Function *> (*maybe_fn), locus, "function");
     175                 :             : 
     176                 :        4007 :   if (auto maybe_extern = mappings.lookup_hir_extern_item (node_id))
     177                 :          36 :     check_extern_call (static_cast<ExternalItem *> (maybe_extern->first),
     178                 :          72 :                        *mappings.lookup_hir_extern_block (maybe_extern->second),
     179                 :             :                        locus);
     180                 :             : }
     181                 :             : 
     182                 :             : static void
     183                 :        2443 : check_target_attr (HIR::Function *fn, location_t locus)
     184                 :             : {
     185                 :        2443 :   if (std::any_of (fn->get_outer_attrs ().begin (),
     186                 :        2443 :                    fn->get_outer_attrs ().end (),
     187                 :          47 :                    [] (const AST::Attribute &attr) {
     188                 :          94 :                      return attr.get_path ().as_string ()
     189                 :          47 :                             == Values::Attributes::TARGET_FEATURE;
     190                 :             :                    }))
     191                 :           2 :     rust_error_at (locus,
     192                 :             :                    "call to function with %<#[target_feature]%> requires "
     193                 :             :                    "unsafe function or block");
     194                 :        2443 : }
     195                 :             : 
     196                 :             : void
     197                 :        8149 : UnsafeChecker::check_function_attr (HirId node_id, location_t locus)
     198                 :             : {
     199                 :        8149 :   if (unsafe_context.is_in_context ())
     200                 :        4142 :     return;
     201                 :             : 
     202                 :        4007 :   auto maybe_fn = mappings.lookup_hir_item (node_id);
     203                 :             : 
     204                 :        4007 :   if (maybe_fn
     205                 :        4007 :       && maybe_fn.value ()->get_item_kind () == Item::ItemKind::Function)
     206                 :        2443 :     check_target_attr (static_cast<Function *> (*maybe_fn), locus);
     207                 :             : }
     208                 :             : 
     209                 :             : void
     210                 :           0 : UnsafeChecker::visit (Lifetime &)
     211                 :           0 : {}
     212                 :             : 
     213                 :             : void
     214                 :           0 : UnsafeChecker::visit (LifetimeParam &)
     215                 :           0 : {}
     216                 :             : 
     217                 :             : void
     218                 :       23000 : UnsafeChecker::visit (PathInExpression &path)
     219                 :             : {
     220                 :       23000 :   NodeId ast_node_id = path.get_mappings ().get_nodeid ();
     221                 :       23000 :   NodeId ref_node_id;
     222                 :             : 
     223                 :       23000 :   if (flag_name_resolution_2_0)
     224                 :             :     {
     225                 :        2412 :       auto &nr_ctx
     226                 :        2412 :         = Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
     227                 :             : 
     228                 :        2412 :       auto resolved = nr_ctx.lookup (ast_node_id);
     229                 :             : 
     230                 :        2412 :       if (!resolved.has_value ())
     231                 :           0 :         return;
     232                 :             : 
     233                 :        2412 :       ref_node_id = resolved.value ();
     234                 :             :     }
     235                 :             :   else
     236                 :             :     {
     237                 :       20588 :       if (!resolver.lookup_resolved_name (ast_node_id, &ref_node_id))
     238                 :             :         return;
     239                 :             :     }
     240                 :             : 
     241                 :       22547 :   if (auto definition_id = mappings.lookup_node_to_hir (ref_node_id))
     242                 :             :     {
     243                 :       22547 :       check_use_of_static (*definition_id, path.get_locus ());
     244                 :             :     }
     245                 :             :   else
     246                 :             :     {
     247                 :           0 :       rust_unreachable ();
     248                 :             :     }
     249                 :             : }
     250                 :             : 
     251                 :             : void
     252                 :           0 : UnsafeChecker::visit (TypePathSegment &)
     253                 :           0 : {}
     254                 :             : 
     255                 :             : void
     256                 :           0 : UnsafeChecker::visit (TypePathSegmentGeneric &)
     257                 :           0 : {}
     258                 :             : 
     259                 :             : void
     260                 :           0 : UnsafeChecker::visit (TypePathSegmentFunction &)
     261                 :           0 : {}
     262                 :             : 
     263                 :             : void
     264                 :           0 : UnsafeChecker::visit (TypePath &)
     265                 :           0 : {}
     266                 :             : 
     267                 :             : void
     268                 :          17 : UnsafeChecker::visit (QualifiedPathInExpression &)
     269                 :          17 : {}
     270                 :             : 
     271                 :             : void
     272                 :           0 : UnsafeChecker::visit (QualifiedPathInType &)
     273                 :           0 : {}
     274                 :             : 
     275                 :             : void
     276                 :       14152 : UnsafeChecker::visit (LiteralExpr &)
     277                 :       14152 : {}
     278                 :             : 
     279                 :             : void
     280                 :        1255 : UnsafeChecker::visit (BorrowExpr &expr)
     281                 :             : {
     282                 :        1255 :   expr.get_expr ().accept_vis (*this);
     283                 :        1255 : }
     284                 :             : 
     285                 :             : void
     286                 :        1755 : UnsafeChecker::visit (DereferenceExpr &expr)
     287                 :             : {
     288                 :        1755 :   TyTy::BaseType *to_deref_type;
     289                 :        1755 :   auto to_deref = expr.get_expr ().get_mappings ().get_hirid ();
     290                 :             : 
     291                 :        1755 :   rust_assert (context.lookup_type (to_deref, &to_deref_type));
     292                 :             : 
     293                 :        1755 :   if (to_deref_type->get_kind () == TyTy::TypeKind::POINTER
     294                 :        1755 :       && !unsafe_context.is_in_context ())
     295                 :           4 :     rust_error_at (expr.get_locus (), "dereference of raw pointer requires "
     296                 :             :                                       "unsafe function or block");
     297                 :        1755 : }
     298                 :             : 
     299                 :             : void
     300                 :           0 : UnsafeChecker::visit (ErrorPropagationExpr &expr)
     301                 :             : {
     302                 :           0 :   expr.get_expr ().accept_vis (*this);
     303                 :           0 : }
     304                 :             : 
     305                 :             : void
     306                 :         345 : UnsafeChecker::visit (NegationExpr &expr)
     307                 :             : {
     308                 :         345 :   expr.get_expr ().accept_vis (*this);
     309                 :         345 : }
     310                 :             : 
     311                 :             : void
     312                 :        2921 : UnsafeChecker::visit (ArithmeticOrLogicalExpr &expr)
     313                 :             : {
     314                 :        2921 :   expr.get_lhs ().accept_vis (*this);
     315                 :        2921 :   expr.get_rhs ().accept_vis (*this);
     316                 :        2921 : }
     317                 :             : 
     318                 :             : void
     319                 :        1092 : UnsafeChecker::visit (ComparisonExpr &expr)
     320                 :             : {
     321                 :        1092 :   expr.get_lhs ().accept_vis (*this);
     322                 :        1092 :   expr.get_rhs ().accept_vis (*this);
     323                 :        1092 : }
     324                 :             : 
     325                 :             : void
     326                 :         323 : UnsafeChecker::visit (LazyBooleanExpr &expr)
     327                 :             : {
     328                 :         323 :   expr.get_lhs ().accept_vis (*this);
     329                 :         323 :   expr.get_rhs ().accept_vis (*this);
     330                 :         323 : }
     331                 :             : 
     332                 :             : void
     333                 :        4538 : UnsafeChecker::visit (TypeCastExpr &expr)
     334                 :             : {
     335                 :        4538 :   expr.get_expr ().accept_vis (*this);
     336                 :        4538 : }
     337                 :             : 
     338                 :             : void
     339                 :        1887 : UnsafeChecker::visit (AssignmentExpr &expr)
     340                 :             : {
     341                 :        1887 :   expr.get_lhs ().accept_vis (*this);
     342                 :        1887 :   expr.get_rhs ().accept_vis (*this);
     343                 :        1887 : }
     344                 :             : 
     345                 :             : void
     346                 :         239 : UnsafeChecker::visit (CompoundAssignmentExpr &expr)
     347                 :             : {
     348                 :         239 :   expr.get_lhs ().accept_vis (*this);
     349                 :         239 :   expr.get_rhs ().accept_vis (*this);
     350                 :         239 : }
     351                 :             : 
     352                 :             : void
     353                 :         201 : UnsafeChecker::visit (GroupedExpr &expr)
     354                 :             : {
     355                 :         201 :   expr.get_expr_in_parens ().accept_vis (*this);
     356                 :         201 : }
     357                 :             : 
     358                 :             : void
     359                 :         263 : UnsafeChecker::visit (ArrayElemsValues &elems)
     360                 :             : {
     361                 :        1584 :   for (auto &elem : elems.get_values ())
     362                 :        1321 :     elem->accept_vis (*this);
     363                 :         263 : }
     364                 :             : 
     365                 :             : void
     366                 :         125 : UnsafeChecker::visit (ArrayElemsCopied &elems)
     367                 :             : {
     368                 :         125 :   elems.get_elem_to_copy ().accept_vis (*this);
     369                 :         125 : }
     370                 :             : 
     371                 :             : void
     372                 :         388 : UnsafeChecker::visit (ArrayExpr &expr)
     373                 :             : {
     374                 :         388 :   expr.get_internal_elements ().accept_vis (*this);
     375                 :         388 : }
     376                 :             : 
     377                 :             : void
     378                 :         233 : UnsafeChecker::visit (ArrayIndexExpr &expr)
     379                 :             : {
     380                 :         233 :   expr.get_array_expr ().accept_vis (*this);
     381                 :         233 :   expr.get_index_expr ().accept_vis (*this);
     382                 :         233 : }
     383                 :             : 
     384                 :             : void
     385                 :         516 : UnsafeChecker::visit (TupleExpr &expr)
     386                 :             : {
     387                 :        1385 :   for (auto &elem : expr.get_tuple_elems ())
     388                 :         869 :     elem->accept_vis (*this);
     389                 :         516 : }
     390                 :             : 
     391                 :             : void
     392                 :         798 : UnsafeChecker::visit (TupleIndexExpr &expr)
     393                 :             : {
     394                 :         798 :   expr.get_tuple_expr ().accept_vis (*this);
     395                 :         798 : }
     396                 :             : 
     397                 :             : void
     398                 :          61 : UnsafeChecker::visit (StructExprStruct &)
     399                 :          61 : {}
     400                 :             : 
     401                 :             : void
     402                 :         232 : UnsafeChecker::visit (StructExprFieldIdentifier &)
     403                 :         232 : {}
     404                 :             : 
     405                 :             : void
     406                 :        2362 : UnsafeChecker::visit (StructExprFieldIdentifierValue &field)
     407                 :             : {
     408                 :        2362 :   field.get_value ().accept_vis (*this);
     409                 :        2362 : }
     410                 :             : 
     411                 :             : void
     412                 :          48 : UnsafeChecker::visit (StructExprFieldIndexValue &field)
     413                 :             : {
     414                 :          48 :   field.get_value ().accept_vis (*this);
     415                 :          48 : }
     416                 :             : 
     417                 :             : void
     418                 :        1074 : UnsafeChecker::visit (StructExprStructFields &expr)
     419                 :             : {
     420                 :        3716 :   for (auto &field : expr.get_fields ())
     421                 :        2642 :     field->accept_vis (*this);
     422                 :        1074 : }
     423                 :             : 
     424                 :             : void
     425                 :           0 : UnsafeChecker::visit (StructExprStructBase &)
     426                 :           0 : {}
     427                 :             : 
     428                 :             : void
     429                 :        9407 : UnsafeChecker::visit (CallExpr &expr)
     430                 :             : {
     431                 :        9407 :   if (!expr.has_fnexpr ())
     432                 :        1258 :     return;
     433                 :             : 
     434                 :        9407 :   NodeId ast_node_id = expr.get_fnexpr ().get_mappings ().get_nodeid ();
     435                 :        9407 :   NodeId ref_node_id;
     436                 :             : 
     437                 :             :   // There are no unsafe types, and functions are defined in the name resolver.
     438                 :             :   // If we can't find the name, then we're dealing with a type and should return
     439                 :             :   // early.
     440                 :        9407 :   if (flag_name_resolution_2_0)
     441                 :             :     {
     442                 :        1027 :       auto &nr_ctx
     443                 :        1027 :         = Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
     444                 :             : 
     445                 :        1027 :       auto resolved = nr_ctx.lookup (ast_node_id);
     446                 :             : 
     447                 :        1027 :       if (!resolved.has_value ())
     448                 :           1 :         return;
     449                 :             : 
     450                 :        1026 :       ref_node_id = resolved.value ();
     451                 :             :     }
     452                 :             :   else
     453                 :             :     {
     454                 :        8380 :       if (!resolver.lookup_resolved_name (ast_node_id, &ref_node_id))
     455                 :             :         return;
     456                 :             :     }
     457                 :             : 
     458                 :        8149 :   if (auto definition_id = mappings.lookup_node_to_hir (ref_node_id))
     459                 :             :     {
     460                 :             :       // At this point we have the function's HIR Id. There are three checks we
     461                 :             :       // must perform:
     462                 :             :       //     1. The function is an unsafe one
     463                 :             :       //     2. The function is an extern one
     464                 :             :       //     3. The function is marked with a target_feature attribute
     465                 :        8149 :       check_function_call (*definition_id, expr.get_locus ());
     466                 :        8149 :       check_function_attr (*definition_id, expr.get_locus ());
     467                 :             : 
     468                 :        8149 :       if (expr.has_params ())
     469                 :       16188 :         for (auto &arg : expr.get_arguments ())
     470                 :        9868 :           arg->accept_vis (*this);
     471                 :             :     }
     472                 :             :   else
     473                 :             :     {
     474                 :           0 :       rust_unreachable ();
     475                 :             :     }
     476                 :             : }
     477                 :             : 
     478                 :             : void
     479                 :        1412 : UnsafeChecker::visit (MethodCallExpr &expr)
     480                 :             : {
     481                 :        1412 :   TyTy::BaseType *method_type;
     482                 :        1412 :   context.lookup_type (expr.get_method_name ().get_mappings ().get_hirid (),
     483                 :             :                        &method_type);
     484                 :        1412 :   if (!method_type || !method_type->is<TyTy::FnType> ())
     485                 :           0 :     return;
     486                 :             : 
     487                 :        1412 :   auto &fn = static_cast<TyTy::FnType &> (*method_type);
     488                 :             : 
     489                 :             :   // FIXME
     490                 :             :   // should probably use the defid lookup instead
     491                 :             :   // tl::optional<HIR::Item *> lookup_defid (DefId id);
     492                 :        1412 :   auto method = mappings.lookup_hir_implitem (fn.get_ref ());
     493                 :        1412 :   if (!unsafe_context.is_in_context () && method)
     494                 :         616 :     check_unsafe_call (static_cast<Function *> (method->first),
     495                 :        1232 :                        expr.get_locus (), "method");
     496                 :             : 
     497                 :        1412 :   expr.get_receiver ().accept_vis (*this);
     498                 :             : 
     499                 :        2008 :   for (auto &arg : expr.get_arguments ())
     500                 :         596 :     arg->accept_vis (*this);
     501                 :             : }
     502                 :             : 
     503                 :             : void
     504                 :        2451 : UnsafeChecker::visit (FieldAccessExpr &expr)
     505                 :             : {
     506                 :        2451 :   expr.get_receiver_expr ().accept_vis (*this);
     507                 :             : 
     508                 :        2451 :   if (unsafe_context.is_in_context ())
     509                 :         520 :     return;
     510                 :             : 
     511                 :        1931 :   TyTy::BaseType *receiver_ty;
     512                 :        1931 :   auto ok = context.lookup_type (
     513                 :        1931 :     expr.get_receiver_expr ().get_mappings ().get_hirid (), &receiver_ty);
     514                 :        1931 :   rust_assert (ok);
     515                 :             : 
     516                 :        1931 :   if (receiver_ty->get_kind () == TyTy::TypeKind::ADT)
     517                 :             :     {
     518                 :        1725 :       auto maybe_union = static_cast<TyTy::ADTType *> (receiver_ty);
     519                 :        1725 :       if (maybe_union->is_union ())
     520                 :           2 :         rust_error_at (
     521                 :             :           expr.get_locus (),
     522                 :             :           "access to union field requires unsafe function or block");
     523                 :             :     }
     524                 :             : }
     525                 :             : 
     526                 :             : void
     527                 :          48 : UnsafeChecker::visit (ClosureExpr &expr)
     528                 :             : {
     529                 :          48 :   expr.get_expr ().accept_vis (*this);
     530                 :          48 : }
     531                 :             : 
     532                 :             : void
     533                 :       18434 : UnsafeChecker::visit (BlockExpr &expr)
     534                 :             : {
     535                 :       38706 :   for (auto &stmt : expr.get_statements ())
     536                 :       20272 :     stmt->accept_vis (*this);
     537                 :             : 
     538                 :       18434 :   if (expr.has_expr ())
     539                 :       12867 :     expr.get_final_expr ().accept_vis (*this);
     540                 :       18434 : }
     541                 :             : 
     542                 :             : void
     543                 :           8 : UnsafeChecker::visit (ContinueExpr &)
     544                 :           8 : {}
     545                 :             : 
     546                 :             : void
     547                 :          86 : UnsafeChecker::visit (BreakExpr &expr)
     548                 :             : {
     549                 :          86 :   if (expr.has_break_expr ())
     550                 :          18 :     expr.get_expr ().accept_vis (*this);
     551                 :          86 : }
     552                 :             : 
     553                 :             : void
     554                 :          69 : UnsafeChecker::visit (RangeFromToExpr &expr)
     555                 :             : {
     556                 :          69 :   expr.get_from_expr ().accept_vis (*this);
     557                 :          69 :   expr.get_to_expr ().accept_vis (*this);
     558                 :          69 : }
     559                 :             : 
     560                 :             : void
     561                 :           8 : UnsafeChecker::visit (RangeFromExpr &expr)
     562                 :             : {
     563                 :           8 :   expr.get_from_expr ().accept_vis (*this);
     564                 :           8 : }
     565                 :             : 
     566                 :             : void
     567                 :           8 : UnsafeChecker::visit (RangeToExpr &expr)
     568                 :             : {
     569                 :           8 :   expr.get_to_expr ().accept_vis (*this);
     570                 :           8 : }
     571                 :             : 
     572                 :             : void
     573                 :           0 : UnsafeChecker::visit (RangeFullExpr &)
     574                 :           0 : {}
     575                 :             : 
     576                 :             : void
     577                 :           8 : UnsafeChecker::visit (RangeFromToInclExpr &expr)
     578                 :             : {
     579                 :           8 :   expr.get_from_expr ().accept_vis (*this);
     580                 :           8 :   expr.get_to_expr ().accept_vis (*this);
     581                 :           8 : }
     582                 :             : 
     583                 :             : void
     584                 :           0 : UnsafeChecker::visit (RangeToInclExpr &expr)
     585                 :             : {
     586                 :           0 :   expr.get_to_expr ().accept_vis (*this);
     587                 :           0 : }
     588                 :             : 
     589                 :             : void
     590                 :         529 : UnsafeChecker::visit (ReturnExpr &expr)
     591                 :             : {
     592                 :         529 :   if (expr.has_return_expr ())
     593                 :         503 :     expr.get_expr ().accept_vis (*this);
     594                 :         529 : }
     595                 :             : 
     596                 :             : void
     597                 :        3425 : UnsafeChecker::visit (UnsafeBlockExpr &expr)
     598                 :             : {
     599                 :        3425 :   unsafe_context.enter (expr.get_mappings ().get_hirid ());
     600                 :             : 
     601                 :        3425 :   expr.get_block_expr ().accept_vis (*this);
     602                 :             : 
     603                 :        3425 :   unsafe_context.exit ();
     604                 :        3425 : }
     605                 :             : 
     606                 :             : void
     607                 :         124 : UnsafeChecker::visit (LoopExpr &expr)
     608                 :             : {
     609                 :         124 :   expr.get_loop_block ().accept_vis (*this);
     610                 :         124 : }
     611                 :             : 
     612                 :             : void
     613                 :          66 : UnsafeChecker::visit (WhileLoopExpr &expr)
     614                 :             : {
     615                 :          66 :   expr.get_predicate_expr ().accept_vis (*this);
     616                 :          66 :   expr.get_loop_block ().accept_vis (*this);
     617                 :          66 : }
     618                 :             : 
     619                 :             : void
     620                 :           0 : UnsafeChecker::visit (WhileLetLoopExpr &expr)
     621                 :             : {
     622                 :           0 :   expr.get_cond ().accept_vis (*this);
     623                 :           0 :   expr.get_loop_block ().accept_vis (*this);
     624                 :           0 : }
     625                 :             : 
     626                 :             : void
     627                 :         387 : UnsafeChecker::visit (IfExpr &expr)
     628                 :             : {
     629                 :         387 :   expr.get_if_condition ().accept_vis (*this);
     630                 :         387 :   expr.get_if_block ().accept_vis (*this);
     631                 :         387 : }
     632                 :             : 
     633                 :             : void
     634                 :         569 : UnsafeChecker::visit (IfExprConseqElse &expr)
     635                 :             : {
     636                 :         569 :   expr.get_if_condition ().accept_vis (*this);
     637                 :         569 :   expr.get_if_block ().accept_vis (*this);
     638                 :         569 :   expr.get_else_block ().accept_vis (*this);
     639                 :         569 : }
     640                 :             : 
     641                 :             : void
     642                 :         459 : UnsafeChecker::visit (MatchExpr &expr)
     643                 :             : {
     644                 :         459 :   expr.get_scrutinee_expr ().accept_vis (*this);
     645                 :             : 
     646                 :        1443 :   for (auto &match_arm : expr.get_match_cases ())
     647                 :         984 :     match_arm.get_expr ().accept_vis (*this);
     648                 :         459 : }
     649                 :             : 
     650                 :             : void
     651                 :           0 : UnsafeChecker::visit (AwaitExpr &)
     652                 :             : {
     653                 :             :   // TODO: Visit expression
     654                 :           0 : }
     655                 :             : 
     656                 :             : void
     657                 :           0 : UnsafeChecker::visit (AsyncBlockExpr &)
     658                 :             : {
     659                 :             :   // TODO: Visit block expression
     660                 :           0 : }
     661                 :             : 
     662                 :             : void
     663                 :          36 : UnsafeChecker::visit (InlineAsm &expr)
     664                 :             : {
     665                 :          36 :   if (unsafe_context.is_in_context ())
     666                 :             :     return;
     667                 :             : 
     668                 :           2 :   rust_error_at (
     669                 :           2 :     expr.get_locus (), ErrorCode::E0133,
     670                 :             :     "use of inline assembly is unsafe and requires unsafe function or block");
     671                 :             : }
     672                 :             : 
     673                 :             : void
     674                 :           2 : UnsafeChecker::visit (LlvmInlineAsm &expr)
     675                 :             : {
     676                 :           2 :   if (unsafe_context.is_in_context ())
     677                 :             :     return;
     678                 :             : 
     679                 :           0 :   rust_error_at (
     680                 :           0 :     expr.get_locus (), ErrorCode::E0133,
     681                 :             :     "use of inline assembly is unsafe and requires unsafe function or block");
     682                 :             : }
     683                 :             : 
     684                 :             : void
     685                 :           0 : UnsafeChecker::visit (TypeParam &)
     686                 :           0 : {}
     687                 :             : 
     688                 :             : void
     689                 :           0 : UnsafeChecker::visit (ConstGenericParam &)
     690                 :           0 : {}
     691                 :             : 
     692                 :             : void
     693                 :           0 : UnsafeChecker::visit (LifetimeWhereClauseItem &)
     694                 :           0 : {}
     695                 :             : 
     696                 :             : void
     697                 :           0 : UnsafeChecker::visit (TypeBoundWhereClauseItem &)
     698                 :           0 : {}
     699                 :             : 
     700                 :             : void
     701                 :         869 : UnsafeChecker::visit (Module &module)
     702                 :             : {
     703                 :        3867 :   for (auto &item : module.get_items ())
     704                 :        2998 :     item->accept_vis (*this);
     705                 :         869 : }
     706                 :             : 
     707                 :             : void
     708                 :           0 : UnsafeChecker::visit (ExternCrate &)
     709                 :           0 : {}
     710                 :             : 
     711                 :             : void
     712                 :           0 : UnsafeChecker::visit (UseTreeGlob &)
     713                 :           0 : {}
     714                 :             : 
     715                 :             : void
     716                 :           0 : UnsafeChecker::visit (UseTreeList &)
     717                 :           0 : {}
     718                 :             : 
     719                 :             : void
     720                 :           0 : UnsafeChecker::visit (UseTreeRebind &)
     721                 :           0 : {}
     722                 :             : 
     723                 :             : void
     724                 :           0 : UnsafeChecker::visit (UseDeclaration &)
     725                 :           0 : {}
     726                 :             : 
     727                 :             : void
     728                 :       12012 : UnsafeChecker::visit (Function &function)
     729                 :             : {
     730                 :       12012 :   auto is_unsafe_fn = function.get_qualifiers ().is_unsafe ();
     731                 :             : 
     732                 :       12012 :   if (is_unsafe_fn)
     733                 :         471 :     unsafe_context.enter (function.get_mappings ().get_hirid ());
     734                 :             : 
     735                 :       12012 :   function.get_definition ().accept_vis (*this);
     736                 :             : 
     737                 :       12012 :   if (is_unsafe_fn)
     738                 :         471 :     unsafe_context.exit ();
     739                 :       12012 : }
     740                 :             : 
     741                 :             : void
     742                 :        1235 : UnsafeChecker::visit (TypeAlias &)
     743                 :             : {
     744                 :             :   // FIXME: What do we need to do to handle type aliasing? Is it possible to
     745                 :             :   // have unsafe types? Type aliases on unsafe functions?
     746                 :        1235 : }
     747                 :             : 
     748                 :             : void
     749                 :        1236 : UnsafeChecker::visit (StructStruct &)
     750                 :        1236 : {}
     751                 :             : 
     752                 :             : void
     753                 :         935 : UnsafeChecker::visit (TupleStruct &)
     754                 :         935 : {}
     755                 :             : 
     756                 :             : void
     757                 :           0 : UnsafeChecker::visit (EnumItem &)
     758                 :           0 : {}
     759                 :             : 
     760                 :             : void
     761                 :           0 : UnsafeChecker::visit (EnumItemTuple &)
     762                 :           0 : {}
     763                 :             : 
     764                 :             : void
     765                 :           0 : UnsafeChecker::visit (EnumItemStruct &)
     766                 :           0 : {}
     767                 :             : 
     768                 :             : void
     769                 :           0 : UnsafeChecker::visit (EnumItemDiscriminant &)
     770                 :           0 : {}
     771                 :             : 
     772                 :             : void
     773                 :         328 : UnsafeChecker::visit (Enum &)
     774                 :         328 : {}
     775                 :             : 
     776                 :             : void
     777                 :          99 : UnsafeChecker::visit (Union &)
     778                 :          99 : {}
     779                 :             : 
     780                 :             : void
     781                 :         547 : UnsafeChecker::visit (ConstantItem &const_item)
     782                 :             : {
     783                 :         547 :   const_item.get_expr ().accept_vis (*this);
     784                 :         547 : }
     785                 :             : 
     786                 :             : void
     787                 :          59 : UnsafeChecker::visit (StaticItem &static_item)
     788                 :             : {
     789                 :          59 :   static_item.get_expr ().accept_vis (*this);
     790                 :          59 : }
     791                 :             : 
     792                 :             : void
     793                 :        1628 : UnsafeChecker::visit (TraitItemFunc &item)
     794                 :             : {
     795                 :        1628 :   if (item.has_definition ())
     796                 :         241 :     item.get_block_expr ().accept_vis (*this);
     797                 :        1628 : }
     798                 :             : 
     799                 :             : void
     800                 :          36 : UnsafeChecker::visit (TraitItemConst &item)
     801                 :             : {
     802                 :          36 :   if (item.has_expr ())
     803                 :           7 :     item.get_expr ().accept_vis (*this);
     804                 :          36 : }
     805                 :             : 
     806                 :             : void
     807                 :         672 : UnsafeChecker::visit (TraitItemType &)
     808                 :         672 : {}
     809                 :             : 
     810                 :             : void
     811                 :        2925 : UnsafeChecker::visit (Trait &trait)
     812                 :             : {
     813                 :             :   // FIXME: Handle unsafe traits
     814                 :        5261 :   for (auto &item : trait.get_trait_items ())
     815                 :        2336 :     item->accept_vis (*this);
     816                 :        2925 : }
     817                 :             : 
     818                 :             : void
     819                 :        4838 : UnsafeChecker::visit (ImplBlock &impl)
     820                 :             : {
     821                 :        4838 :   bool safe = !impl.is_unsafe ();
     822                 :             :   // Check for unsafe-only attributes on generics and lifetimes
     823                 :        4838 :   if (safe)
     824                 :        5822 :     for (auto &parm : impl.get_generic_params ())
     825                 :             :       {
     826                 :        1055 :         for (auto o_attr : parm->get_outer_attrs ())
     827                 :             :           {
     828                 :           4 :             rust_assert (!o_attr.is_inner_attribute ());
     829                 :             : 
     830                 :           4 :             Rust::AST::SimplePath path = o_attr.get_path ();
     831                 :           4 :             if (path == Values::Attributes::MAY_DANGLE)
     832                 :           4 :               rust_error_at (
     833                 :             :                 o_attr.get_locus (), ErrorCode::E0569,
     834                 :             :                 "use of %<may_dangle%> is unsafe and requires unsafe impl");
     835                 :           8 :           }
     836                 :             :       }
     837                 :             : 
     838                 :       11840 :   for (auto &item : impl.get_impl_items ())
     839                 :        7002 :     item->accept_vis (*this);
     840                 :        4838 : }
     841                 :             : 
     842                 :             : void
     843                 :           2 : UnsafeChecker::visit (ExternalStaticItem &)
     844                 :           2 : {}
     845                 :             : 
     846                 :             : void
     847                 :        2078 : UnsafeChecker::visit (ExternalFunctionItem &)
     848                 :        2078 : {}
     849                 :             : 
     850                 :             : void
     851                 :           0 : UnsafeChecker::visit (ExternalTypeItem &)
     852                 :           0 : {}
     853                 :             : 
     854                 :             : void
     855                 :        1247 : UnsafeChecker::visit (ExternBlock &block)
     856                 :             : {
     857                 :             :   // FIXME: Do we need to do this?
     858                 :        3327 :   for (auto &item : block.get_extern_items ())
     859                 :        2080 :     item->accept_vis (*this);
     860                 :        1247 : }
     861                 :             : 
     862                 :             : void
     863                 :           0 : UnsafeChecker::visit (LiteralPattern &)
     864                 :           0 : {}
     865                 :             : 
     866                 :             : void
     867                 :           0 : UnsafeChecker::visit (IdentifierPattern &)
     868                 :           0 : {}
     869                 :             : 
     870                 :             : void
     871                 :           0 : UnsafeChecker::visit (WildcardPattern &)
     872                 :           0 : {}
     873                 :             : 
     874                 :             : void
     875                 :           0 : UnsafeChecker::visit (RangePatternBoundLiteral &)
     876                 :           0 : {}
     877                 :             : 
     878                 :             : void
     879                 :           0 : UnsafeChecker::visit (RangePatternBoundPath &)
     880                 :           0 : {}
     881                 :             : 
     882                 :             : void
     883                 :           0 : UnsafeChecker::visit (RangePatternBoundQualPath &)
     884                 :           0 : {}
     885                 :             : 
     886                 :             : void
     887                 :           0 : UnsafeChecker::visit (RangePattern &)
     888                 :           0 : {}
     889                 :             : 
     890                 :             : void
     891                 :           0 : UnsafeChecker::visit (ReferencePattern &)
     892                 :           0 : {}
     893                 :             : 
     894                 :             : void
     895                 :           0 : UnsafeChecker::visit (StructPatternFieldTuplePat &)
     896                 :           0 : {}
     897                 :             : 
     898                 :             : void
     899                 :           0 : UnsafeChecker::visit (StructPatternFieldIdentPat &)
     900                 :           0 : {}
     901                 :             : 
     902                 :             : void
     903                 :           0 : UnsafeChecker::visit (StructPatternFieldIdent &)
     904                 :           0 : {}
     905                 :             : 
     906                 :             : void
     907                 :           0 : UnsafeChecker::visit (StructPattern &)
     908                 :           0 : {}
     909                 :             : 
     910                 :             : void
     911                 :           0 : UnsafeChecker::visit (TupleStructItemsNoRange &)
     912                 :           0 : {}
     913                 :             : 
     914                 :             : void
     915                 :           0 : UnsafeChecker::visit (TupleStructItemsRange &)
     916                 :           0 : {}
     917                 :             : 
     918                 :             : void
     919                 :           0 : UnsafeChecker::visit (TupleStructPattern &)
     920                 :           0 : {}
     921                 :             : 
     922                 :             : void
     923                 :           0 : UnsafeChecker::visit (TuplePatternItemsMultiple &)
     924                 :           0 : {}
     925                 :             : 
     926                 :             : void
     927                 :           0 : UnsafeChecker::visit (TuplePatternItemsRanged &)
     928                 :           0 : {}
     929                 :             : 
     930                 :             : void
     931                 :           0 : UnsafeChecker::visit (TuplePattern &)
     932                 :           0 : {}
     933                 :             : 
     934                 :             : void
     935                 :           0 : UnsafeChecker::visit (SlicePattern &)
     936                 :           0 : {}
     937                 :             : 
     938                 :             : void
     939                 :           0 : UnsafeChecker::visit (AltPattern &)
     940                 :           0 : {}
     941                 :             : 
     942                 :             : void
     943                 :          52 : UnsafeChecker::visit (EmptyStmt &)
     944                 :          52 : {}
     945                 :             : 
     946                 :             : void
     947                 :       12374 : UnsafeChecker::visit (LetStmt &stmt)
     948                 :             : {
     949                 :       12374 :   if (stmt.has_init_expr ())
     950                 :       11198 :     stmt.get_init_expr ().accept_vis (*this);
     951                 :       12374 : }
     952                 :             : 
     953                 :             : void
     954                 :        7477 : UnsafeChecker::visit (ExprStmt &stmt)
     955                 :             : {
     956                 :        7477 :   stmt.get_expr ().accept_vis (*this);
     957                 :        7477 : }
     958                 :             : 
     959                 :             : void
     960                 :           0 : UnsafeChecker::visit (TraitBound &)
     961                 :           0 : {}
     962                 :             : 
     963                 :             : void
     964                 :           0 : UnsafeChecker::visit (ImplTraitType &)
     965                 :           0 : {}
     966                 :             : 
     967                 :             : void
     968                 :           0 : UnsafeChecker::visit (TraitObjectType &)
     969                 :           0 : {}
     970                 :             : 
     971                 :             : void
     972                 :           0 : UnsafeChecker::visit (ParenthesisedType &)
     973                 :           0 : {}
     974                 :             : 
     975                 :             : void
     976                 :           0 : UnsafeChecker::visit (TupleType &)
     977                 :           0 : {}
     978                 :             : 
     979                 :             : void
     980                 :           0 : UnsafeChecker::visit (NeverType &)
     981                 :           0 : {}
     982                 :             : 
     983                 :             : void
     984                 :           0 : UnsafeChecker::visit (RawPointerType &)
     985                 :           0 : {}
     986                 :             : 
     987                 :             : void
     988                 :           0 : UnsafeChecker::visit (ReferenceType &)
     989                 :           0 : {}
     990                 :             : 
     991                 :             : void
     992                 :           0 : UnsafeChecker::visit (ArrayType &)
     993                 :           0 : {}
     994                 :             : 
     995                 :             : void
     996                 :           0 : UnsafeChecker::visit (SliceType &)
     997                 :           0 : {}
     998                 :             : 
     999                 :             : void
    1000                 :           0 : UnsafeChecker::visit (InferredType &)
    1001                 :           0 : {}
    1002                 :             : 
    1003                 :             : void
    1004                 :           0 : UnsafeChecker::visit (BareFunctionType &)
    1005                 :           0 : {}
    1006                 :             : 
    1007                 :             : } // namespace HIR
    1008                 :             : } // namespace Rust
        

Generated by: LCOV version 2.1-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.