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 9449 : ASTValidation::visit (AST::Lifetime &lifetime)
29 : {
30 9449 : auto name = lifetime.get_lifetime_name ();
31 47245 : auto valid = std::set<std::string>{"static", "_"};
32 9449 : auto &keywords = Values::Keywords::keywords;
33 :
34 9449 : if (valid.find (name) == valid.end ()
35 9449 : && keywords.find (name) != keywords.end ())
36 2 : rust_error_at (lifetime.get_locus (), "lifetimes cannot use keyword names");
37 :
38 9449 : AST::ContextualASTVisitor::visit (lifetime);
39 9449 : }
40 :
41 : void
42 63 : ASTValidation::visit (AST::LoopLabel &label)
43 : {
44 63 : auto name = label.get_lifetime ().get_lifetime_name ();
45 63 : auto lifetime_name = '\'' + name;
46 63 : auto &keywords = Values::Keywords::keywords;
47 63 : 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 63 : }
55 :
56 : void
57 585 : ASTValidation::visit (AST::ConstantItem &const_item)
58 : {
59 585 : 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 585 : AST::ContextualASTVisitor::visit (const_item);
65 585 : }
66 :
67 : void
68 111 : ASTValidation::visit (AST::Union &item)
69 : {
70 111 : if (item.get_variants ().empty ())
71 1 : rust_error_at (item.get_locus (), "unions cannot have zero fields");
72 :
73 111 : AST::ContextualASTVisitor::visit (item);
74 111 : }
75 :
76 : void
77 18746 : ASTValidation::visit (AST::Function &function)
78 : {
79 18746 : const auto &qualifiers = function.get_qualifiers ();
80 18746 : if (qualifiers.is_default () && ctx.peek () != Kind::INHERENT_IMPL
81 18769 : && 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 18746 : 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 18746 : if (qualifiers.is_const ()
90 18746 : && (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 18746 : if (qualifiers.is_async ()
96 18746 : && (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 18746 : if (ctx.peek () != Kind::TRAIT && ctx.peek () != Kind::TRAIT_IMPL
102 9226 : && ctx.peek () != Kind::INHERENT_IMPL && function.has_self_param ())
103 0 : rust_error_at (
104 0 : function.get_self_param ().get_locus (),
105 : "%<self%> parameter is only allowed in associated functions");
106 :
107 18746 : if (function.is_external ())
108 : {
109 2499 : if (function.has_body ())
110 1 : rust_error_at (function.get_locus (), "cannot have a body");
111 :
112 2499 : auto ¶ms = function.get_function_params ();
113 :
114 2499 : if (params.size () == 1 && function.is_variadic ())
115 1 : rust_error_at (function.get_locus (),
116 : "C-variadic function must be declared with at least one "
117 : "named argument");
118 :
119 6166 : for (auto it = params.begin (); it != params.end (); it++)
120 : {
121 3667 : if (it->get ()->is_variadic () && it + 1 != params.end ())
122 1 : rust_error_at (
123 : it->get ()->get_locus (),
124 : "%<...%> must be the last argument of a C-variadic function");
125 :
126 : // if functional parameter
127 3667 : if (!it->get ()->is_self () && !it->get ()->is_variadic ())
128 : {
129 2834 : auto ¶m = static_cast<AST::FunctionParam &> (**it);
130 2834 : auto kind = param.get_pattern ().get_pattern_kind ();
131 :
132 2834 : if (kind != AST::Pattern::Kind::Identifier
133 2834 : && kind != AST::Pattern::Kind::Wildcard)
134 0 : rust_error_at (it->get ()->get_locus (), ErrorCode::E0130,
135 : "pattern not allowed in foreign function");
136 : }
137 : }
138 : }
139 :
140 : else
141 : {
142 16247 : if (!function.has_body ())
143 : {
144 1707 : if (ctx.peek () == Kind::INHERENT_IMPL
145 1707 : || ctx.peek () == Kind::TRAIT_IMPL)
146 3 : rust_error_at (function.get_locus (),
147 : "associated function in %<impl%> without body");
148 1704 : else if (ctx.peek () != Kind::TRAIT)
149 1 : rust_error_at (function.get_locus (),
150 : "free function without a body");
151 : }
152 16247 : auto &function_params = function.get_function_params ();
153 32361 : for (auto it = function_params.begin (); it != function_params.end ();
154 16114 : it++)
155 : {
156 16114 : if (it->get ()->is_variadic ())
157 4 : rust_error_at (
158 : it->get ()->get_locus (),
159 : "only foreign or %<unsafe extern \"C\"%> functions may "
160 : "be C-variadic");
161 : }
162 : }
163 :
164 18746 : AST::ContextualASTVisitor::visit (function);
165 18746 : }
166 :
167 : void
168 3895 : ASTValidation::visit (AST::Trait &trait)
169 : {
170 3895 : if (trait.is_auto ())
171 : {
172 20 : if (trait.has_generics ())
173 1 : rust_error_at (trait.get_generic_params ()[0]->get_locus (),
174 : ErrorCode::E0567,
175 : "auto traits cannot have generic parameters");
176 20 : if (trait.has_type_param_bounds ())
177 1 : rust_error_at (trait.get_type_param_bounds ()[0]->get_locus (),
178 : ErrorCode::E0568,
179 : "auto traits cannot have super traits");
180 20 : if (trait.has_trait_items ())
181 : {
182 1 : rust_error_at (trait.get_identifier ().get_locus (), ErrorCode::E0380,
183 : "auto traits cannot have methods or associated items");
184 6 : for (const auto &item : trait.get_trait_items ())
185 5 : Error::Hint (item->get_locus (), "remove this item").emit ();
186 : }
187 : }
188 :
189 3895 : AST::ContextualASTVisitor::visit (trait);
190 3895 : }
191 :
192 : void
193 1309 : ASTValidation::visit (AST::Module &module)
194 : {
195 1309 : if (module.get_unsafety () == Unsafety::Unsafe)
196 1 : rust_error_at (module.get_locus (), "module cannot be declared unsafe");
197 :
198 1309 : AST::ContextualASTVisitor::visit (module);
199 1309 : }
200 :
201 : } // namespace Rust
|