1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
// Copyright (C) 2020-2023 Free Software Foundation, Inc.
// This file is part of GCC.
// GCC is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3, or (at your option) any later
// version.
// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
// You should have received a copy of the GNU General Public License
// along with GCC; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
#include "rust-ast-validation.h"
#include "rust-common.h"
#include "rust-diagnostics.h"
#include "rust-item.h"
#include "rust-keyword-values.h"
namespace Rust {
void
ASTValidation::visit (AST::Lifetime &lifetime)
{
auto name = lifetime.get_lifetime_name ();
auto valid = std::set<std::string>{"static", "_"};
auto &keywords = Values::Keywords::keywords;
if (valid.find (name) == valid.end ()
&& keywords.find (name) != keywords.end ())
rust_error_at (lifetime.get_locus (), "lifetimes cannot use keyword names");
AST::ContextualASTVisitor::visit (lifetime);
}
void
ASTValidation::visit (AST::LoopLabel &label)
{
auto name = label.get_lifetime ().get_lifetime_name ();
auto lifetime_name = '\'' + name;
auto &keywords = Values::Keywords::keywords;
if (keywords.find (name) != keywords.end ())
rust_error_at (label.get_lifetime ().get_locus (), "invalid label name %qs",
lifetime_name.c_str ());
// WARNING: Do not call ContextualASTVisitor, we don't want to visit the
// lifetime
// Maybe we should refactor LoopLabel instead ?
}
void
ASTValidation::visit (AST::ConstantItem &const_item)
{
if (!const_item.has_expr () && context.back () != Context::TRAIT_IMPL)
{
rust_error_at (const_item.get_locus (),
"associated constant in %<impl%> without body");
}
AST::ContextualASTVisitor::visit (const_item);
}
void
ASTValidation::visit (AST::ExternalFunctionItem &item)
{
auto ¶ms = item.get_function_params ();
if (params.size () == 1 && params[0].is_variadic ())
rust_error_at (
params[0].get_locus (),
"C-variadic function must be declared with at least one named argument");
for (auto it = params.begin (); it != params.end (); it++)
if (it->is_variadic () && it + 1 != params.end ())
rust_error_at (
it->get_locus (),
"%<...%> must be the last argument of a C-variadic function");
AST::ContextualASTVisitor::visit (item);
}
void
ASTValidation::visit (AST::Union &item)
{
if (item.get_variants ().empty ())
rust_error_at (item.get_locus (), "unions cannot have zero fields");
AST::ContextualASTVisitor::visit (item);
}
void
ASTValidation::visit (AST::Function &function)
{
std::set<Context> valid_context
= {Context::INHERENT_IMPL, Context::TRAIT_IMPL};
const auto &qualifiers = function.get_qualifiers ();
if (qualifiers.is_async () && qualifiers.is_const ())
rust_error_at (function.get_locus (),
"functions cannot be both %<const%> and %<async%>");
if (valid_context.find (context.back ()) == valid_context.end ()
&& function.has_self_param ())
rust_error_at (
function.get_self_param ()->get_locus (),
"%<self%> parameter is only allowed in associated functions");
if (function.is_variadic ())
rust_error_at (
function.get_function_params ().back ()->get_locus (),
"only foreign or %<unsafe extern \"C\"%> functions may be C-variadic");
AST::ContextualASTVisitor::visit (function);
}
void
ASTValidation::visit (AST::Trait &trait)
{
if (trait.is_auto ())
{
if (trait.has_generics ())
rust_error_at (trait.get_generic_params ()[0]->get_locus (),
ErrorCode::E0567,
"auto traits cannot have generic parameters");
if (trait.has_type_param_bounds ())
rust_error_at (trait.get_type_param_bounds ()[0]->get_locus (),
ErrorCode::E0568,
"auto traits cannot have super traits");
if (trait.has_trait_items ())
{
rust_error_at (trait.get_identifier ().get_locus (), ErrorCode::E0380,
"auto traits cannot have methods or associated items");
for (const auto &item : trait.get_trait_items ())
Error::Hint (item->get_locus (), "remove this item").emit ();
}
}
AST::ContextualASTVisitor::visit (trait);
}
void
ASTValidation::visit (AST::Module &module)
{
if (module.get_unsafety () == Unsafety::Unsafe)
rust_error_at (module.get_locus (), "module cannot be declared unsafe");
AST::ContextualASTVisitor::visit (module);
}
} // namespace Rust
|