Debian Codegraph @

gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc

raw
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-privacy-reporter.h"
20 #include "rust-session-manager.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-immutable-name-resolution-context.h"
26
27 namespace Rust {
28 namespace Privacy {
29
30 PrivacyReporter::PrivacyReporter (
31 Analysis::Mappings &mappings, Resolver::Resolver &resolver,
32 const Rust::Resolver::TypeCheckContext &ty_ctx)
33 : mappings (mappings), resolver (resolver), ty_ctx (ty_ctx),
34 current_module (tl::nullopt)
35 {}
36
37 // Find a proc_macro, proc_macro_derive or proc_macro_attribute
38 // attribute in a vector of attribute
39 static tl::optional<std::string>
40 find_proc_macro_attribute (const AST::AttrVec &outer_attrs)
41 {
42 for (auto &a : outer_attrs)
43 {
44 auto &segments = a.get_path ().get_segments ();
45 if (segments.size () != 1)
46 continue;
47 auto name = segments.at (0).get_segment_name ();
48 if (name == Values::Attributes::PROC_MACRO
49 || name == Values::Attributes::PROC_MACRO_ATTRIBUTE
50 || name == Values::Attributes::PROC_MACRO_DERIVE)
51 return name;
52 }
53
54 return tl::nullopt;
55 }
56
57 // Common check on crate items when dealing with 'proc-macro' crate type.
58 static void
59 proc_macro_privacy_check (std::unique_ptr<HIR::Item> &item)
60 {
61 if (item->get_hir_kind () == HIR::Node::BaseKind::VIS_ITEM)
62 {
63 auto attribute = find_proc_macro_attribute (item->get_outer_attrs ());
64
65 bool pub_item = static_cast<HIR::VisItem *> (item.get ())
66 ->get_visibility ()
67 .is_public ();
68
69 if (pub_item && !attribute.has_value ()) // Public non proc-macro
70 rust_error_at (
71 item->get_locus (),
72 "%<proc-macro%> crate types currently cannot export any "
73 "items other than functions tagged with %<#[proc_macro]%>, "
74 "%<#[proc_macro_derive]%> or %<#[proc_macro_attribute]%>");
75 else if (!pub_item && attribute.has_value ()) // Private proc-macro
76 rust_error_at (item->get_locus (),
77 "functions tagged with %<#[%s]%> must be %<pub%>",
78 attribute.value ().c_str ());
79 }
80 }
81
82 void
83 PrivacyReporter::go (HIR::Crate &crate)
84 {
85 for (auto &item : crate.get_items ())
86 {
87 if (Session::get_instance ().options.is_proc_macro ())
88 proc_macro_privacy_check (item);
89 item->accept_vis (*this);
90 }
91 }
92
93 static bool
94 is_child_module (Analysis::Mappings &mappings, NodeId parent,
95 NodeId possible_child)
96 {
97 if (flag_name_resolution_2_0)
98 {
99 auto &nr_ctx
100 = Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
101
102 return nr_ctx.values.is_module_descendant (parent, possible_child);
103 }
104
105 auto children = mappings.lookup_module_children (parent);
106
107 if (!children)
108 return false;
109
110 // Visit all toplevel children
111 for (auto &child : *children)
112 if (child == possible_child)
113 return true;
114
115 // Now descend recursively in the child module tree
116 for (auto &child : *children)
117 if (is_child_module (mappings, child, possible_child))
118 return true;
119
120 return false;
121 }
122
123 // FIXME: This function needs a lot of refactoring
124 void
125 PrivacyReporter::check_for_privacy_violation (const NodeId &use_id,
126 const location_t locus)
127 {
128 NodeId ref_node_id = UNKNOWN_NODEID;
129
130 if (flag_name_resolution_2_0)
131 {
132 auto &nr_ctx
133 = Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
134
135 if (auto id = nr_ctx.lookup (use_id))
136 ref_node_id = *id;
137 }
138 // FIXME: Don't assert here - we might be dealing with a type
139 else if (!resolver.lookup_resolved_name (use_id, &ref_node_id))
140 resolver.lookup_resolved_type (use_id, &ref_node_id);
141
142 // FIXME: Assert here. For now, we return since this causes issues when
143 // checking inferred types (#1260)
144 // rust_assert (ref_node_id != UNKNOWN_NODEID);
145 if (ref_node_id == UNKNOWN_NODEID)
146 return;
147
148 auto vis = mappings.lookup_visibility (ref_node_id);
149
150 // FIXME: Can we really return here if the item has no visibility?
151 if (!vis)
152 return;
153
154 auto valid = true;
155
156 switch (vis->get_kind ())
157 {
158 case ModuleVisibility::Public:
159 break;
160 case ModuleVisibility::Restricted: {
161 // If we are in the crate, everything is restricted correctly, but we
162 // can't get a module for it
163 if (!current_module.has_value ())
164 return;
165
166 auto module = mappings.lookup_defid (vis->get_module_id ()).value ();
167
168 auto mod_node_id = module->get_mappings ().get_nodeid ();
169
170 // We are in the module referenced by the pub(restricted) visibility.
171 // This is valid
172 if (mod_node_id == current_module.value ())
173 break;
174
175 // FIXME: This needs a LOT of TLC: hinting about the definition, a
176 // string to say if it's a module, function, type, etc...
177 if (!is_child_module (mappings, mod_node_id, current_module.value ()))
178 valid = false;
179 }
180 break;
181 case ModuleVisibility::Unknown:
182 rust_unreachable ();
183 break;
184 }
185
186 if (!valid)
187 {
188 rich_location richloc (line_table, locus);
189 richloc.add_fixit_replace ("item is private");
190 rust_error_at (richloc, ErrorCode::E0603,
191 "definition is private in this context");
192 }
193 }
194
195 void
196 PrivacyReporter::check_base_type_privacy (Analysis::NodeMapping &node_mappings,
197 const TyTy::BaseType *ty,
198 const location_t locus)
199 {
200 // Avoids repeating commong argument such as `use_id` or `locus` since we're
201 // doing a lot of recursive calls here
202 auto recursive_check
203 = [this, &node_mappings, &locus] (const TyTy::BaseType *ty) {
204 return check_base_type_privacy (node_mappings, ty, locus);
205 };
206
207 switch (ty->get_kind ())
208 {
209 // These "simple" types are our stop condition
210 case TyTy::BOOL:
211 case TyTy::CHAR:
212 case TyTy::INT:
213 case TyTy::UINT:
214 case TyTy::FLOAT:
215 case TyTy::USIZE:
216 case TyTy::ISIZE:
217 case TyTy::ADT:
218 case TyTy::STR: {
219 auto ref_id = ty->get_ref ();
220 if (auto lookup_id = mappings.lookup_hir_to_node (ref_id))
221 return check_for_privacy_violation (*lookup_id, locus);
222 rust_unreachable ();
223 }
224 case TyTy::REF:
225 return recursive_check (
226 static_cast<const TyTy::ReferenceType *> (ty)->get_base ());
227 case TyTy::POINTER:
228 return recursive_check (
229 static_cast<const TyTy::PointerType *> (ty)->get_base ());
230 case TyTy::ARRAY:
231 return recursive_check (
232 static_cast<const TyTy::ArrayType *> (ty)->get_element_type ());
233 case TyTy::SLICE:
234 return recursive_check (
235 static_cast<const TyTy::SliceType *> (ty)->get_element_type ());
236 case TyTy::FNPTR:
237 for (auto &param : static_cast<const TyTy::FnPtr *> (ty)->get_params ())
238 recursive_check (param.get_tyty ());
239 return recursive_check (
240 static_cast<const TyTy::FnPtr *> (ty)->get_return_type ());
241 case TyTy::TUPLE:
242 for (auto &param :
243 static_cast<const TyTy::TupleType *> (ty)->get_fields ())
244 recursive_check (param.get_tyty ());
245 return;
246 case TyTy::PLACEHOLDER: {
247 const auto p = static_cast<const TyTy::PlaceholderType *> (ty);
248 if (!p->can_resolve ())
249 return;
250 return recursive_check (p->resolve ());
251 }
252 case TyTy::PROJECTION:
253 return recursive_check (
254 static_cast<const TyTy::ProjectionType *> (ty)->get ());
255 case TyTy::CLOSURE:
256 rust_sorry_at (locus, "privacy pass for closures is not handled yet");
257 break;
258
259 // If we're dealing with a generic param, there's nothing we should be
260 // doing here
261 case TyTy::PARAM:
262 // We are dealing with a function definition that has been assigned
263 // somewhere else. Nothing to resolve privacy-wise other than the actual
264 // function, which is resolved as an expression
265 case TyTy::FNDEF:
266 // FIXME: Can we really not resolve Dynamic types here? Shouldn't we have
267 // a look at the path and perform proper privacy analysis?
268 case TyTy::DYNAMIC:
269 // The never type is builtin and always available
270 case TyTy::NEVER:
271 // We shouldn't have inference types here, ever
272 case TyTy::INFER:
273 return;
274 case TyTy::OPAQUE:
275 return;
276 case TyTy::ERROR:
277 return;
278 }
279 }
280
281 void
282 PrivacyReporter::check_type_privacy (const HIR::Type &type)
283 {
284 TyTy::BaseType *lookup = nullptr;
285 rust_assert (ty_ctx.lookup_type (type.get_mappings ().get_hirid (), &lookup));
286
287 auto node_mappings = type.get_mappings ();
288 return check_base_type_privacy (node_mappings, lookup, type.get_locus ());
289 }
290
291 void
292 PrivacyReporter::visit (HIR::PathInExpression &path)
293 {
294 check_for_privacy_violation (path.get_mappings ().get_nodeid (),
295 path.get_locus ());
296 }
297
298 void
299 PrivacyReporter::visit (HIR::TypePathSegmentFunction &)
300 {
301 // FIXME: Do we need to do anything for this?
302 }
303
304 void
305 PrivacyReporter::visit (HIR::InlineAsm &)
306 {}
307
308 void
309 PrivacyReporter::visit (HIR::TypePath &path)
310 {
311 check_for_privacy_violation (path.get_mappings ().get_nodeid (),
312 path.get_locus ());
313 }
314
315 void
316 PrivacyReporter::visit (HIR::QualifiedPathInExpression &path)
317 {
318 check_for_privacy_violation (path.get_mappings ().get_nodeid (),
319 path.get_locus ());
320 }
321
322 void
323 PrivacyReporter::visit (HIR::QualifiedPathInType &path)
324 {
325 check_for_privacy_violation (path.get_mappings ().get_nodeid (),
326 path.get_locus ());
327 }
328
329 void
330 PrivacyReporter::visit (HIR::LiteralExpr &)
331 {
332 // Literals cannot contain any sort of privacy violation
333 }
334
335 void
336 PrivacyReporter::visit (HIR::BorrowExpr &expr)
337 {
338 expr.get_expr ().accept_vis (*this);
339 }
340
341 void
342 PrivacyReporter::visit (HIR::DereferenceExpr &expr)
343 {
344 expr.get_expr ().accept_vis (*this);
345 }
346
347 void
348 PrivacyReporter::visit (HIR::ErrorPropagationExpr &expr)
349 {
350 expr.get_expr ().accept_vis (*this);
351 }
352
353 void
354 PrivacyReporter::visit (HIR::NegationExpr &expr)
355 {
356 expr.get_expr ().accept_vis (*this);
357 }
358
359 void
360 PrivacyReporter::visit (HIR::ArithmeticOrLogicalExpr &expr)
361 {
362 expr.get_lhs ().accept_vis (*this);
363 expr.get_rhs ().accept_vis (*this);
364 }
365
366 void
367 PrivacyReporter::visit (HIR::ComparisonExpr &expr)
368 {
369 expr.get_lhs ().accept_vis (*this);
370 expr.get_rhs ().accept_vis (*this);
371 }
372
373 void
374 PrivacyReporter::visit (HIR::LazyBooleanExpr &expr)
375 {
376 expr.get_lhs ().accept_vis (*this);
377 expr.get_rhs ().accept_vis (*this);
378 }
379
380 void
381 PrivacyReporter::visit (HIR::TypeCastExpr &expr)
382 {
383 expr.get_expr ().accept_vis (*this);
384 }
385
386 void
387 PrivacyReporter::visit (HIR::AssignmentExpr &expr)
388 {
389 expr.get_lhs ().accept_vis (*this);
390 expr.get_rhs ().accept_vis (*this);
391 }
392
393 void
394 PrivacyReporter::visit (HIR::CompoundAssignmentExpr &expr)
395 {
396 expr.get_lhs ().accept_vis (*this);
397 expr.get_rhs ().accept_vis (*this);
398 }
399
400 void
401 PrivacyReporter::visit (HIR::GroupedExpr &expr)
402 {
403 expr.get_expr_in_parens ().accept_vis (*this);
404 }
405
406 void
407 PrivacyReporter::visit (HIR::ArrayExpr &expr)
408 {
409 HIR::ArrayElems &elements = expr.get_internal_elements ();
410 switch (elements.get_array_expr_type ())
411 {
412 case HIR::ArrayElems::ArrayExprType::VALUES: {
413 auto &elems = static_cast<HIR::ArrayElemsValues &> (elements);
414 for (auto &value : elems.get_values ())
415 value->accept_vis (*this);
416 }
417 return;
418
419 case HIR::ArrayElems::ArrayExprType::COPIED:
420 auto &elems = static_cast<HIR::ArrayElemsCopied &> (elements);
421 elems.get_elem_to_copy ().accept_vis (*this);
422 }
423 }
424
425 void
426 PrivacyReporter::visit (HIR::ArrayIndexExpr &expr)
427 {
428 expr.get_array_expr ().accept_vis (*this);
429 expr.get_index_expr ().accept_vis (*this);
430 }
431
432 void
433 PrivacyReporter::visit (HIR::TupleExpr &expr)
434 {
435 for (auto &value : expr.get_tuple_elems ())
436 value->accept_vis (*this);
437 }
438
439 void
440 PrivacyReporter::visit (HIR::TupleIndexExpr &expr)
441 {
442 expr.get_tuple_expr ().accept_vis (*this);
443 }
444
445 void
446 PrivacyReporter::visit (HIR::StructExprStruct &)
447 {
448 // FIXME: We need to check the visibility of the type it refers to here
449 }
450
451 void
452 PrivacyReporter::visit (HIR::StructExprFieldIdentifier &)
453 {}
454
455 void
456 PrivacyReporter::visit (HIR::StructExprFieldIdentifierValue &field)
457 {
458 field.get_value ().accept_vis (*this);
459 }
460
461 void
462 PrivacyReporter::visit (HIR::StructExprFieldIndexValue &field)
463 {
464 field.get_value ().accept_vis (*this);
465 }
466
467 void
468 PrivacyReporter::visit (HIR::StructExprStructFields &expr)
469 {
470 for (auto &field : expr.get_fields ())
471 field->accept_vis (*this);
472 }
473
474 void
475 PrivacyReporter::visit (HIR::CallExpr &expr)
476 {
477 expr.get_fnexpr ().accept_vis (*this);
478
479 for (auto &param : expr.get_arguments ())
480 param->accept_vis (*this);
481 }
482
483 void
484 PrivacyReporter::visit (HIR::MethodCallExpr &expr)
485 {
486 expr.get_receiver ().accept_vis (*this);
487
488 for (auto &param : expr.get_arguments ())
489 param->accept_vis (*this);
490 }
491
492 void
493 PrivacyReporter::visit (HIR::FieldAccessExpr &expr)
494 {
495 expr.get_receiver_expr ().accept_vis (*this);
496
497 // FIXME: We should also check if the field is public?
498 }
499
500 void
501 PrivacyReporter::visit (HIR::ClosureExpr &)
502 {
503 // Not handled yet
504 }
505
506 void
507 PrivacyReporter::visit (HIR::BlockExpr &expr)
508 {
509 for (auto &stmt : expr.get_statements ())
510 stmt->accept_vis (*this);
511
512 if (expr.has_final_expr ())
513 expr.get_final_expr ().accept_vis (*this);
514 }
515
516 void
517 PrivacyReporter::visit (HIR::ContinueExpr &)
518 {}
519
520 void
521 PrivacyReporter::visit (HIR::BreakExpr &expr)
522 {
523 if (expr.has_break_expr ())
524 expr.get_expr ().accept_vis (*this);
525 }
526
527 void
528 PrivacyReporter::visit (HIR::RangeFromToExpr &expr)
529 {
530 expr.get_from_expr ().accept_vis (*this);
531 expr.get_to_expr ().accept_vis (*this);
532 }
533
534 void
535 PrivacyReporter::visit (HIR::RangeFromExpr &expr)
536 {
537 expr.get_from_expr ().accept_vis (*this);
538 }
539
540 void
541 PrivacyReporter::visit (HIR::RangeToExpr &expr)
542 {
543 expr.get_to_expr ().accept_vis (*this);
544 }
545
546 void
547 PrivacyReporter::visit (HIR::RangeFullExpr &)
548 {}
549
550 void
551 PrivacyReporter::visit (HIR::RangeFromToInclExpr &expr)
552 {
553 expr.get_from_expr ().accept_vis (*this);
554 expr.get_to_expr ().accept_vis (*this);
555 }
556
557 void
558 PrivacyReporter::visit (HIR::RangeToInclExpr &)
559 {
560 // Not handled yet
561 }
562
563 void
564 PrivacyReporter::visit (HIR::ReturnExpr &expr)
565 {
566 if (expr.has_expr ())
567 expr.get_expr ().accept_vis (*this);
568 }
569
570 void
571 PrivacyReporter::visit (HIR::UnsafeBlockExpr &expr)
572 {
573 expr.get_block_expr ().accept_vis (*this);
574 }
575
576 void
577 PrivacyReporter::visit (HIR::LoopExpr &expr)
578 {
579 expr.get_loop_block ().accept_vis (*this);
580 }
581
582 void
583 PrivacyReporter::visit (HIR::WhileLoopExpr &expr)
584 {
585 expr.get_predicate_expr ().accept_vis (*this);
586 expr.get_loop_block ().accept_vis (*this);
587 }
588
589 void
590 PrivacyReporter::visit (HIR::WhileLetLoopExpr &expr)
591 {
592 expr.get_cond ().accept_vis (*this);
593 expr.get_loop_block ().accept_vis (*this);
594 }
595
596 void
597 PrivacyReporter::visit (HIR::IfExpr &expr)
598 {
599 expr.get_if_condition ().accept_vis (*this);
600 expr.get_if_block ().accept_vis (*this);
601 }
602
603 void
604 PrivacyReporter::visit (HIR::IfExprConseqElse &expr)
605 {
606 expr.get_if_condition ().accept_vis (*this);
607 expr.get_if_block ().accept_vis (*this);
608 expr.get_else_block ().accept_vis (*this);
609 }
610
611 void
612 PrivacyReporter::visit (HIR::MatchExpr &expr)
613 {
614 expr.get_scrutinee_expr ().accept_vis (*this);
615 }
616
617 void
618 PrivacyReporter::visit (HIR::AwaitExpr &)
619 {
620 // Not handled yet
621 }
622
623 void
624 PrivacyReporter::visit (HIR::AsyncBlockExpr &)
625 {
626 // Not handled yet
627 }
628
629 void
630 PrivacyReporter::visit (HIR::Module &module)
631 {
632 // FIXME: We also need to think about module privacy
633
634 auto old_module = current_module;
635 current_module = module.get_mappings ().get_nodeid ();
636
637 for (auto &item : module.get_items ())
638 item->accept_vis (*this);
639
640 current_module = old_module;
641 }
642
643 void
644 PrivacyReporter::visit (HIR::ExternCrate &)
645 {}
646
647 void
648 PrivacyReporter::visit (HIR::UseDeclaration &)
649 {
650 // FIXME: Is there anything we need to do here?
651 }
652
653 void
654 PrivacyReporter::visit (HIR::Function &function)
655 {
656 for (auto &param : function.get_function_params ())
657 check_type_privacy (param.get_type ());
658
659 function.get_definition ().accept_vis (*this);
660 }
661
662 void
663 PrivacyReporter::visit (HIR::TypeAlias &)
664 {
665 // TODO: Check the type here
666 }
667
668 void
669 PrivacyReporter::visit (HIR::StructStruct &)
670 {
671 // TODO: Check the type of all fields
672 }
673
674 void
675 PrivacyReporter::visit (HIR::TupleStruct &)
676 {
677 // TODO: Check the type of all fields
678 }
679
680 void
681 PrivacyReporter::visit (HIR::EnumItem &)
682 {
683 // TODO: Check the type of all variants
684 }
685
686 void
687 PrivacyReporter::visit (HIR::EnumItemTuple &)
688 {
689 // TODO: Check the type
690 }
691
692 void
693 PrivacyReporter::visit (HIR::EnumItemStruct &)
694 {
695 // TODO: Check the type
696 }
697
698 void
699 PrivacyReporter::visit (HIR::EnumItemDiscriminant &)
700 {}
701
702 void
703 PrivacyReporter::visit (HIR::Enum &)
704 {}
705
706 void
707 PrivacyReporter::visit (HIR::Union &)
708 {
709 // TODO: Check the type
710 }
711
712 void
713 PrivacyReporter::visit (HIR::ConstantItem &const_item)
714 {
715 // TODO: We need to visit the type
716 const_item.get_expr ().accept_vis (*this);
717 }
718
719 void
720 PrivacyReporter::visit (HIR::StaticItem &static_item)
721 {
722 // TODO: We need to visit the type
723 static_item.get_expr ().accept_vis (*this);
724 }
725
726 void
727 PrivacyReporter::visit (HIR::Trait &)
728 {
729 // FIXME: We need to be an ItemVisitor as well
730 // for (auto &item : trait.get_trait_items ())
731 // item->accept_vis (*this);
732 }
733
734 void
735 PrivacyReporter::visit (HIR::ImplBlock &impl)
736 {
737 for (auto &item : impl.get_impl_items ())
738 item->accept_vis (*this);
739 }
740
741 void
742 PrivacyReporter::visit (HIR::ExternBlock &)
743 {
744 // FIXME: We need to be an ItemVisitor as well
745 // for (auto &block: block.get_extern_items ())
746 // item->accept_vis (*this);
747 }
748
749 void
750 PrivacyReporter::visit (HIR::EmptyStmt &)
751 {}
752
753 void
754 PrivacyReporter::visit (HIR::LetStmt &stmt)
755 {
756 if (stmt.has_type ())
757 check_type_privacy (stmt.get_type ());
758
759 if (stmt.has_init_expr ())
760 stmt.get_init_expr ().accept_vis (*this);
761 }
762
763 void
764 PrivacyReporter::visit (HIR::ExprStmt &stmt)
765 {
766 stmt.get_expr ().accept_vis (*this);
767 }
768
769 } // namespace Privacy
770 } // namespace Rust