Line data Source code
1 : // Copyright (C) 2020-2026 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 184782 : ASTValidation::visit (AST::Lifetime &lifetime)
29 : {
30 184782 : auto name = lifetime.get_lifetime_name ();
31 923910 : auto valid = std::set<std::string>{"static", "_"};
32 184782 : auto &keywords = Values::Keywords::keywords;
33 :
34 184782 : if (valid.find (name) == valid.end ()
35 184782 : && keywords.find (name) != keywords.end ())
36 2 : rust_error_at (lifetime.get_locus (), "lifetimes cannot use keyword names");
37 :
38 184782 : AST::ContextualASTVisitor::visit (lifetime);
39 184782 : }
40 :
41 : void
42 120 : ASTValidation::visit (AST::LoopLabel &label)
43 : {
44 120 : auto name = label.get_lifetime ().get_lifetime_name ();
45 120 : auto lifetime_name = '\'' + name;
46 120 : auto &keywords = Values::Keywords::keywords;
47 120 : 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 120 : }
55 :
56 : void
57 9114 : ASTValidation::visit (AST::ConstantItem &const_item)
58 : {
59 9114 : if (!const_item.has_expr () && ctx.peek () != Kind::TRAIT)
60 : {
61 2 : rust_error_at (const_item.get_locus (),
62 : "associated constant in %<impl%> without body");
63 : }
64 9114 : AST::ContextualASTVisitor::visit (const_item);
65 9114 : }
66 :
67 : void
68 139 : ASTValidation::visit (AST::Union &item)
69 : {
70 139 : if (item.get_variants ().empty ())
71 1 : rust_error_at (item.get_locus (), "unions cannot have zero fields");
72 :
73 139 : AST::ContextualASTVisitor::visit (item);
74 139 : }
75 :
76 : void
77 171647 : ASTValidation::visit (AST::Function &function)
78 : {
79 171647 : const auto &qualifiers = function.get_qualifiers ();
80 171647 : if (qualifiers.is_default () && ctx.peek () != Kind::INHERENT_IMPL
81 172026 : && ctx.peek () != Kind::TRAIT_IMPL)
82 1 : rust_error_at (
83 : function.get_locus (),
84 : "%<default%> is only allowed on items within %<impl%> blocks");
85 171647 : if (qualifiers.is_async () && qualifiers.is_const ())
86 1 : rust_error_at (function.get_locus (),
87 : "functions cannot be both %<const%> and %<async%>");
88 :
89 171647 : if (qualifiers.is_const ()
90 171647 : && (ctx.peek () == Kind::TRAIT_IMPL || ctx.peek () == Kind::TRAIT))
91 2 : rust_error_at (function.get_locus (), ErrorCode::E0379,
92 : "functions in traits cannot be declared %<const%>");
93 :
94 : // may change soon
95 171647 : if (qualifiers.is_async ()
96 171647 : && (ctx.peek () == Kind::TRAIT_IMPL || ctx.peek () == Kind::TRAIT))
97 3 : rust_error_at (function.get_locus (), ErrorCode::E0706,
98 : "functions in traits cannot be declared %<async%>");
99 :
100 : // if not an associated function but has a self parameter
101 171647 : if (ctx.peek () != Kind::TRAIT && ctx.peek () != Kind::TRAIT_IMPL
102 45444 : && ctx.peek () != Kind::INHERENT_IMPL && function.has_self_param ())
103 1 : rust_error_at (
104 1 : function.get_self_param ().get_locus (),
105 : "%<self%> parameter is only allowed in associated functions");
106 :
107 : // functions without bodies can only have very specific
108 : // kinds of patterns in their parameters
109 171647 : auto is_param_complex_with_loc
110 32130 : = [] (AST::Param ¶m) -> tl::optional<location_t> {
111 32130 : tl::optional<location_t> ret;
112 :
113 32130 : if (param.is_self () || param.is_variadic ())
114 4320 : return ret;
115 :
116 27810 : auto &fn_param = static_cast<AST::FunctionParam &> (param);
117 27810 : if (!fn_param.has_name ())
118 0 : return ret;
119 :
120 27810 : auto &pat = fn_param.get_pattern ();
121 27810 : ret = pat.get_locus ();
122 27810 : auto kind = pat.get_pattern_kind ();
123 27810 : if (kind == AST::Pattern::Kind::Identifier)
124 : {
125 27579 : auto &ident_pat = static_cast<AST::IdentifierPattern &> (pat);
126 27579 : if (!ident_pat.get_is_ref () && !ident_pat.get_is_mut ()
127 55154 : && !ident_pat.has_subpattern ())
128 : ret = tl::nullopt;
129 : }
130 231 : else if (kind == AST::Pattern::Kind::Wildcard)
131 : ret = tl::nullopt;
132 :
133 27810 : return ret;
134 : };
135 :
136 171647 : if (function.is_external ())
137 : {
138 13119 : if (function.has_body ())
139 1 : rust_error_at (function.get_locus (), "cannot have a body");
140 :
141 13119 : auto ¶ms = function.get_function_params ();
142 :
143 13119 : if (params.size () == 1 && function.is_variadic ())
144 1 : rust_error_at (function.get_locus (),
145 : "C-variadic function must be declared with at least one "
146 : "named argument");
147 :
148 39317 : for (auto it = params.begin (); it != params.end (); it++)
149 : {
150 26198 : if (it->get ()->is_variadic () && it + 1 != params.end ())
151 1 : rust_error_at (
152 : it->get ()->get_locus (),
153 : "%<...%> must be the last argument of a C-variadic function");
154 :
155 26198 : if (auto pat_loc = is_param_complex_with_loc (**it))
156 5 : rust_error_at (*pat_loc, ErrorCode::E0130,
157 : "pattern not allowed in foreign function");
158 : }
159 : }
160 : else
161 : {
162 158528 : if (!function.has_body ())
163 : {
164 4080 : if (ctx.peek () == Kind::INHERENT_IMPL
165 4080 : || ctx.peek () == Kind::TRAIT_IMPL)
166 3 : rust_error_at (function.get_locus (),
167 : "associated function in %<impl%> without body");
168 4077 : else if (ctx.peek () != Kind::TRAIT)
169 3 : rust_error_at (function.get_locus (),
170 : "free function without a body");
171 : }
172 158528 : auto &function_params = function.get_function_params ();
173 434025 : for (auto it = function_params.begin (); it != function_params.end ();
174 275497 : it++)
175 : {
176 275497 : if (it->get ()->is_variadic ())
177 4 : rust_error_at (
178 : it->get ()->get_locus (),
179 : "only foreign or %<unsafe extern \"C\"%> functions may "
180 : "be C-variadic");
181 :
182 275497 : if (!function.has_body ())
183 5932 : if (auto pat_loc = is_param_complex_with_loc (**it))
184 5 : rust_error_at (*pat_loc, ErrorCode::E0642,
185 : "pattern not allowed in function without body");
186 : }
187 : }
188 :
189 171647 : AST::ContextualASTVisitor::visit (function);
190 171647 : }
191 :
192 : void
193 6274 : ASTValidation::visit (AST::Trait &trait)
194 : {
195 6274 : if (trait.is_auto ())
196 : {
197 78 : if (trait.has_generics ())
198 1 : rust_error_at (trait.get_generic_params ()[0]->get_locus (),
199 : ErrorCode::E0567,
200 : "auto traits cannot have generic parameters");
201 78 : if (trait.has_type_param_bounds ())
202 1 : rust_error_at (trait.get_type_param_bounds ()[0]->get_locus (),
203 : ErrorCode::E0568,
204 : "auto traits cannot have super traits");
205 78 : if (trait.has_trait_items ())
206 : {
207 1 : rust_error_at (trait.get_identifier ().get_locus (), ErrorCode::E0380,
208 : "auto traits cannot have methods or associated items");
209 6 : for (const auto &item : trait.get_trait_items ())
210 5 : Error::Hint (item->get_locus (), "remove this item").emit ();
211 : }
212 : }
213 :
214 6274 : AST::ContextualASTVisitor::visit (trait);
215 6274 : }
216 :
217 : void
218 4464 : ASTValidation::visit (AST::Module &module)
219 : {
220 4464 : if (module.get_unsafety () == Unsafety::Unsafe)
221 1 : rust_error_at (module.get_locus (), "module cannot be declared unsafe");
222 :
223 4464 : AST::ContextualASTVisitor::visit (module);
224 4464 : }
225 :
226 : void
227 287 : ASTValidation::visit (AST::SlicePattern &pattern)
228 : {
229 : // TODO: store/use first rest pattern location?
230 : // for nicer errors
231 287 : bool had_rest = false;
232 :
233 482 : auto note_rest = [&] (location_t locus) {
234 195 : if (had_rest)
235 0 : rust_error_at (locus, "%<..%> can only be used once per slice pattern");
236 195 : had_rest = true;
237 482 : };
238 :
239 898 : for (auto &pat : pattern.get_patterns ())
240 : {
241 611 : if (pat->get_pattern_kind () == AST::Pattern::Kind::Rest)
242 : {
243 146 : note_rest (pat->get_locus ());
244 : }
245 465 : else if (pat->get_pattern_kind () == AST::Pattern::Kind::Identifier)
246 : {
247 349 : auto &ident_pat = static_cast<AST::IdentifierPattern &> (*pat);
248 349 : if (ident_pat.has_subpattern ())
249 : {
250 49 : auto &ident_sub_pat = ident_pat.get_subpattern ();
251 49 : if (ident_sub_pat.get_pattern_kind () == AST::Pattern::Kind::Rest)
252 49 : note_rest (ident_sub_pat.get_locus ());
253 : }
254 : }
255 : }
256 287 : }
257 :
258 : } // namespace Rust
|