Branch data Line data Source code
1 : : // Copyright (C) 2020-2024 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-ast-validation.h"
20 : : #include "rust-common.h"
21 : : #include "rust-diagnostics.h"
22 : : #include "rust-item.h"
23 : : #include "rust-keyword-values.h"
24 : :
25 : : namespace Rust {
26 : :
27 : : void
28 : 5533 : ASTValidation::visit (AST::Lifetime &lifetime)
29 : : {
30 : 5533 : auto name = lifetime.get_lifetime_name ();
31 : 16599 : auto valid = std::set<std::string>{"static", "_"};
32 : 5533 : auto &keywords = Values::Keywords::keywords;
33 : :
34 : 5533 : if (valid.find (name) == valid.end ()
35 : 5533 : && keywords.find (name) != keywords.end ())
36 : 2 : rust_error_at (lifetime.get_locus (), "lifetimes cannot use keyword names");
37 : :
38 : 5533 : AST::ContextualASTVisitor::visit (lifetime);
39 : 5533 : }
40 : :
41 : : void
42 : 149 : ASTValidation::visit (AST::LoopLabel &label)
43 : : {
44 : 149 : auto name = label.get_lifetime ().get_lifetime_name ();
45 : 149 : auto lifetime_name = '\'' + name;
46 : 149 : auto &keywords = Values::Keywords::keywords;
47 : 149 : if (keywords.find (name) != keywords.end ())
48 : 6 : rust_error_at (label.get_lifetime ().get_locus (), "invalid label name %qs",
49 : : lifetime_name.c_str ());
50 : :
51 : : // WARNING: Do not call ContextualASTVisitor, we don't want to visit the
52 : : // lifetime
53 : : // Maybe we should refactor LoopLabel instead ?
54 : 149 : }
55 : :
56 : : void
57 : 473 : ASTValidation::visit (AST::ConstantItem &const_item)
58 : : {
59 : 473 : if (!const_item.has_expr () && context.back () != Context::TRAIT_IMPL)
60 : : {
61 : 1 : rust_error_at (const_item.get_locus (),
62 : : "associated constant in %<impl%> without body");
63 : : }
64 : 473 : AST::ContextualASTVisitor::visit (const_item);
65 : 473 : }
66 : :
67 : : void
68 : 1588 : ASTValidation::visit (AST::ExternalFunctionItem &item)
69 : : {
70 : 1588 : auto ¶ms = item.get_function_params ();
71 : :
72 : 1588 : if (params.size () == 1 && params[0].is_variadic ())
73 : 1 : rust_error_at (
74 : 1 : params[0].get_locus (),
75 : : "C-variadic function must be declared with at least one named argument");
76 : :
77 : 4223 : for (auto it = params.begin (); it != params.end (); it++)
78 : 2635 : if (it->is_variadic () && it + 1 != params.end ())
79 : 1 : rust_error_at (
80 : : it->get_locus (),
81 : : "%<...%> must be the last argument of a C-variadic function");
82 : :
83 : 1588 : AST::ContextualASTVisitor::visit (item);
84 : 1588 : }
85 : :
86 : : void
87 : 97 : ASTValidation::visit (AST::Union &item)
88 : : {
89 : 97 : if (item.get_variants ().empty ())
90 : 1 : rust_error_at (item.get_locus (), "unions cannot have zero fields");
91 : :
92 : 97 : AST::ContextualASTVisitor::visit (item);
93 : 97 : }
94 : :
95 : : void
96 : 9685 : ASTValidation::visit (AST::Function &function)
97 : : {
98 : 9685 : const auto &qualifiers = function.get_qualifiers ();
99 : 9685 : if (qualifiers.is_async () && qualifiers.is_const ())
100 : 1 : rust_error_at (function.get_locus (),
101 : : "functions cannot be both %<const%> and %<async%>");
102 : :
103 : 9685 : if (qualifiers.is_const ()
104 : 9685 : && (context.back () == Context::TRAIT_IMPL
105 : 611 : || context.back () == Context::TRAIT))
106 : 2 : rust_error_at (function.get_locus (), ErrorCode::E0379,
107 : : "functions in traits cannot be declared %<const%>");
108 : :
109 : : // may change soon
110 : 9685 : if (qualifiers.is_async ()
111 : 9685 : && (context.back () == Context::TRAIT_IMPL
112 : 6 : || context.back () == Context::TRAIT))
113 : 3 : rust_error_at (function.get_locus (), ErrorCode::E0706,
114 : : "functions in traits cannot be declared %<async%>");
115 : :
116 : : // if not an associated function but has a self parameter
117 : 9685 : if (context.back () != Context::TRAIT
118 : : && context.back () != Context::TRAIT_IMPL
119 : : && context.back () != Context::INHERENT_IMPL
120 : 5194 : && function.has_self_param ())
121 : 0 : rust_error_at (
122 : 0 : function.get_self_param ()->get_locus (),
123 : : "%<self%> parameter is only allowed in associated functions");
124 : :
125 : 9685 : if (!function.has_body ())
126 : : {
127 : 866 : if (context.back () == Context::INHERENT_IMPL
128 : 866 : || context.back () == Context::TRAIT_IMPL)
129 : 3 : rust_error_at (function.get_locus (),
130 : : "associated function in %<impl%> without body");
131 : 863 : else if (context.back () != Context::TRAIT)
132 : 1 : rust_error_at (function.get_locus (), "free function without a body");
133 : : }
134 : :
135 : 9685 : auto &function_params = function.get_function_params ();
136 : 16854 : for (auto it = function_params.begin (); it != function_params.end (); it++)
137 : : {
138 : 7169 : if (it->get ()->is_variadic ())
139 : 4 : rust_error_at (it->get ()->get_locus (),
140 : : "only foreign or %<unsafe extern \"C\"%> functions may "
141 : : "be C-variadic");
142 : : }
143 : :
144 : 9685 : AST::ContextualASTVisitor::visit (function);
145 : 9685 : }
146 : :
147 : : void
148 : 2106 : ASTValidation::visit (AST::Trait &trait)
149 : : {
150 : 2106 : if (trait.is_auto ())
151 : : {
152 : 7 : if (trait.has_generics ())
153 : 1 : rust_error_at (trait.get_generic_params ()[0]->get_locus (),
154 : : ErrorCode::E0567,
155 : : "auto traits cannot have generic parameters");
156 : 7 : if (trait.has_type_param_bounds ())
157 : 1 : rust_error_at (trait.get_type_param_bounds ()[0]->get_locus (),
158 : : ErrorCode::E0568,
159 : : "auto traits cannot have super traits");
160 : 7 : if (trait.has_trait_items ())
161 : : {
162 : 1 : rust_error_at (trait.get_identifier ().get_locus (), ErrorCode::E0380,
163 : : "auto traits cannot have methods or associated items");
164 : 6 : for (const auto &item : trait.get_trait_items ())
165 : 5 : Error::Hint (item->get_locus (), "remove this item").emit ();
166 : : }
167 : : }
168 : :
169 : 2106 : AST::ContextualASTVisitor::visit (trait);
170 : 2106 : }
171 : :
172 : : void
173 : 508 : ASTValidation::visit (AST::Module &module)
174 : : {
175 : 508 : if (module.get_unsafety () == Unsafety::Unsafe)
176 : 1 : rust_error_at (module.get_locus (), "module cannot be declared unsafe");
177 : :
178 : 508 : AST::ContextualASTVisitor::visit (module);
179 : 508 : }
180 : :
181 : : } // namespace Rust
|