Skip to content

Conversation

@kkjeer
Copy link
Contributor

@kkjeer kkjeer commented Nov 6, 2019

(See #684)
Emit an error for each usage of a free type variable in the declaration of a static variable. The following:

struct S _For_any(T, U) { };
_For_any(T, U) void f(void) {
  static struct S<T, U> s;
}

will emit the following:

error: static variable 's' has a type that uses a type variable bound in an enclosing scope (type is 'struct S<T, U>' and type variable is 'T')
note: type variable 'T' declared here
error: static variable 's' has a type that uses a type variable bound in an enclosing scope (type is 'struct S<T, U>' and type variable is 'U')
note: type variable 'U' declared here

These errors are emitted at the location of the static variable. For example:
static-free-type-variable-error-message-1

Future work: disallow free type variables in assignments to static variables (see #717)

Testing:

@kkjeer kkjeer requested review from dtarditi and mgrang November 6, 2019 03:14
Copy link
Member

@dtarditi dtarditi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have some suggestions.

def warn_vector_long_decl_spec_combination : Warning<
"Use of 'long' with '__vector' is deprecated">, InGroup<Deprecated>;

def err_static_decl_uses_free_type_variable : Error<
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the error messages needs to be changed to be more understandable by C programmers. Most of them won't know what a free type variable. How about something like the following? "static variable cannot have a type variable bound by an enclosing scope"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you need to include the type and indicate in the message that the problem is with type. Something akin to:

"type for static variable '%0' cannot use a type variable '%1' that is bound by an enclosing scope"

or

"static variable '%0' has a type '%1' that uses a type variable bound by an enclosing scope'


/// Returns the list of free typedef declarations referenced in the given type.
/// Typedef declarations enable more readable diagnostics than type variable types.
std::vector<const TypedefNameDecl *> findTypedefDecls(QualType Tpe) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of Type, I'd suggest Ty as the variable name.

Copy link
Member

@dtarditi dtarditi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the error message needs to indicate that the problem is with the use of type variable in the type of the variable.

def warn_vector_long_decl_spec_combination : Warning<
"Use of 'long' with '__vector' is deprecated">, InGroup<Deprecated>;

def err_static_decl_uses_free_type_variable : Error<
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you need to include the type and indicate in the message that the problem is with type. Something akin to:

"type for static variable '%0' cannot use a type variable '%1' that is bound by an enclosing scope"

or

"static variable '%0' has a type '%1' that uses a type variable bound by an enclosing scope'

Copy link
Member

@dtarditi dtarditi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Thanks!

@kkjeer kkjeer merged commit e3be202 into master Nov 8, 2019
@dtarditi dtarditi deleted the issue684-static-type-variables branch November 16, 2019 07:20
Machiry pushed a commit to Machiry/checkedc-clang that referenced this pull request Jan 21, 2022
This pull requests extends array bounds inference to support inferring lower
bounds for array pointers and inserting using Checked C range bounds.

For example:

    char simple_lower_bound(int *a, int l) {
      int *b = a;
      while (b - a < l && *b != 42)
        b++;
      return b - a < l;
    }

3C can now infer bounds for b even though a standard count bound would be
invalidated by the increment b++.

    char simple_lower_bound(_Array_ptr<int> a : count(l), int l) {
      _Array_ptr<int> b : bounds(a, a + l) = a;
      while (b - a < l && *b != 42)
        b++;
      return b - a < l;
    }

The inference is also able to automatically fatten pointers by generating lower
bounds where none exists in the source code. 

Co-authored-by: Matt McCutchen (Correct Computation) <matt@correctcomputation.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants